Replace Debug::{key_with, value_with} with DebugMap::entry_with (original) (raw)

Proposal

Replace the (currently nightly) methods for printing the Debug repr of a map's key & value with one method for both.

Problem statement

To ensure that the methods key_with & value_with are called in the right order, DebugMap remembers whether key_with was called without an accompanying value_with call, and if the order is violated, it panics. This setup makes it easier to commit a logical error that will crash the app at runtime, and unnecessarily increases code size with a conditional panic

Solution sketch

impl DebugMap { fn entry_with<K, V>(&mut self, k: K, v: V) -> &mut Self where K: FnOnce(&mut Formatter<'_>) -> std::fmt::Result, V: FnOnce(&mut Formatter<'_>) -> std::fmt::Result; }

This approach:

This sample of code on Github suggests that practically all of DebugMap::key_with invocations are immediately followed by an invocation of DebugMap::value_with, which raises a question of "do they really have to be 2 separate functions?"

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution: