use std::fmt::{Formatter, Display};
trait NumericCode
where Self: Sized
{
fn code(&self) -> &Display;
}
impl <T: NumericCode> Display for T {
fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
write!(f, "{}", self.code() )
}
}
enum Binary {
Zero,
One
}
impl NumericCode for Binary{
fn code(&self) -> &Display {
match self {
Binary::Zero => &"0",
Binary::One => &"1",
}
}
}
fn main () {
println!("0={}", Binary::Zero)
}
It seems Display trait, being non-local for the crate, can not be implemented for a T, even though T is limited to a local trait NumericCode.
Can the errors 0210 & 0119 be evaded?
-
For instance, I need the implementation for all Sized crate-local types only. Can I specify this somehow, to exclude the possibility of conflicts with other crates?
-
Or can T be limited with some !Display constraint, meaning - "impl only for those T which do not have other Display impls"?