#bounds #type #generic #specialization

nightly negative-type-bound

provides negative type bound

1 unstable release

0.1.0 Jan 10, 2022

#14 in #specialization

MIT license

4KB

crates.io

this crate uses feature negative_impls and auto_traits

You may want to do something like

struct Foo<T>(T);

impl<T: From<U>, U> From<Foo<U>> for Foo<T> {
    fn from(v: Foo<U>) -> Self {
        Self(T::from(v.0))
    }
}

but it wont work because it conflicts with implementation impl<T> From<T> for T {} provided by core.

so we have to bound Foo<T> != Foo<U>

this crate provides trait NotEqual

(T, U): NotEqual is equivalent to T != U

so this will work

struct Foo<T>(T);

impl<T: From<U>, U> From<Foo<U>> for Foo<T> where (Foo<T>, Foo<U>): NotEqual {
    fn from(v: Foo<U>) -> Self {
        Self(T::from(v.0))
    }
}

more simply...

struct Foo<T>(T);

impl<T: From<U>, U> From<Foo<U>> for Foo<T> where (T, U): NotEqual {
    fn from(v: Foo<U>) -> Self {
        Self(T::from(v.0))
    }
}

this crate also provides Equal

No runtime deps