/// A trait for converting a value to a `String` via [`Debug`].
///
/// This trait is automatically implemented for any type which implements the
/// [`Debug`] trait. As such, `ToDebug` shouldn't be implemented directly:
/// [`Debug`] should be implemented instead, and you get the `ToDebug`
/// implementation for free.
///
/// [`Debug`]: ../../std/fmt/trait.Debug.html
#[unstable(feature = "todebug", reason = "new API", issue="...")]
pub trait ToDebug {
/// Converts the given value to a `String` via `Debug`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let s = "hello";
/// let hello = String::from("\"hello\"");
///
/// assert_eq!(hello, s.to_debug());
/// ```
#[unstable(feature = "todebug", reason = "new API", issue="...")]
fn to_debug(&self) -> String;
}
/// # Panics
///
/// In this implementation, the `to_debug` method panics
/// if the `Debug` implementation returns an error.
/// This indicates an incorrect `Debug` implementation
/// since `fmt::Write for String` never returns an error itself.
#[unstable(feature = "todebug", reason = "new API", issue="...")]
impl<T: fmt::Debug + ?Sized> ToDebug for T {
#[inline]
default fn to_debug(&self) -> String {
use fmt::Write;
let mut buf = String::new();
buf.write_fmt(format_args!("{:?}", self))
.expect("a Debug implementation returned an error unexpectedly");
buf.shrink_to_fit();
buf
}
}
Can't we just add a method on Debug to do this conversion? I'm pretty sure ToString was a mistake, and I don't see the reason to add yet another trait for this. Ideally, to_string would be on Display, but it's too late for that (because there is likely some code that relies on ToString, directly). Also, making a new trait would create some confusion, should I implement ToDebug directly, or implement Debug. The correct answer is alwaysDebug because it is far more applicable.
Note that neither Debug nor Display are in the prelude. I believe that this is because their fmt methods conflict. If .to_debug() is on Debug, it will require importing std::fmt::Debug to use the method.
Interestingly, if fn to_string(&self) -> String had been a method on Display rather than its own trait, we wouldn't have needed specialization to specialize str/String/Cow<str>::to_string.
x.to_debug() is just minimally more convenient than format!("{:?}", x). Debug also supports formatting, e.g. pretty-printing mode, so you'd need a method or an arg for format!("{:#?}", x).
I don't think it's worth making the change, especially that Debug output as a string is mainly used for ugly hacks like parsing the debug output to get private details of a type.
"mainly" is subjective of course. It's my impression from questions on forums and mentions elsewhere on the net that I've seen.
Note that you described a case for generating a nice Debug format, and printing it. That doesn't necessarily require capturing of the Debug output as a String, and especially not to a degree that would require a convenience function for it.