I’m quite interested in this as well. I have half an RFC written up, I’m just trying to figure out how the default case should be. I assume anything Show should automatically be able to be Repr. However, we can’t do impl<T: Show> Repr for T, because then nothing can have specialized Repr vs Show.
The one solution I think works well is to, instead of adding a new trait, add a new method to Show, that has a default implementation. This way, all Show can be repred, and anything that wants to specialize can override the default method.
It’d look something like this:
trait Show for Sized? {
fn fmt(&self, &mut fmt::Formatter) -> fmt::Result;
fn repr(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.fmt(f)
}
}
And an example implementation:
impl Show for MaybeOwned {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_slice().fmt(f)
}
fn repr(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Slice(s) => write!(f, "Slice({:?})", s),
Owned(s) => write!(f, "Owned({:?})", s),
}
}
}
Ideally, str would use write!(f, "\"{}\"", self), leave us with Slice("Foo").