Conflicting implementations of trait in Rust

Solution 1:

The fact that &'a str does not implement Into<i32> is not taken into account, because there is no guarantee that it couldn't be added later. This would then break your code.

So if this were allowed the possible breakage would make it harder to add implementations to library traits.

Unfortunately I couldn't find documentation for that, neither in The Rust Programming Language Book nor in the Reference Manual.

The best I could find is RFC 1023, which says that a crate [...] cannot rely that Type: !Trait holds unless Type or Trait is local.

Solution 2:

I found a workaround using a marker trait. No nightly or experimental features necessary. The trick is that I define the marker trait in my crate and do not export it, so it is impossible for an upstream crate to define the marker on classes other than the ones I implement it on.

Below the marker trait is Numeric.

I use this so that I can implement Into for anything that can be converted into an f64, but also for strings in a separate impl, and other types as well.

The Numeric trait must be pub because they are warning that future versions will disallow a private Trait in a public interface.


use std::convert::Into;

pub trait Numeric {}
impl Numeric for f64 {}
impl Numeric for f32 {}
impl Numeric for i64 {}
impl Numeric for i32 {}
impl Numeric for i16 {}
impl Numeric for i8 {}
impl Numeric for isize {}
impl Numeric for u64 {}
impl Numeric for u32 {}
impl Numeric for u16 {}
impl Numeric for u8 {}
impl Numeric for usize {}


pub trait UiId {
    fn push(&self);
}

impl<'a> UiId for &'a str {
    fn push(&self) {}
}

impl<T: Into<i32> + Numeric> UiId for T {
    fn push(&self) {}
}