Impl external trait for non-local parameter (limited only by the other local trait)

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?

  1. 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?

  2. Or can T be limited with some !Display constraint, meaning - "impl only for those T which do not have other Display impls"?

https://github.com/rust-lang/rust/issues/48869

1 Like

I've read the issue above. It started with something alike, but ended discussing Box. Didn't quite understand the conclusion: is it a confusing error text or a problem with rust to be solved eventually? Or is the subj code erroneous? If so, how can it be revised (other than impl-ing the Display directly for all the many different types through my crate, where problem originated)?