Trait spidermeme::NotSameTypeAs[][src]

pub trait NotSameTypeAs<T>: NotSameTypeAs<T> { }
Expand description

NotSameTypeAs<T> is a marker trait that is automatically implemented for all types that do not alias to the same type T. Lifetimes are not considered.

Examples

use spidermeme::NotSameTypeAs;
use static_assertions::{assert_impl_all, assert_not_impl_any};

assert_impl_all!(i32: NotSameTypeAs<i64>);
assert_not_impl_any!(i32: NotSameTypeAs<i32>);

Different types with identical structures aren’t equal:

use spidermeme::NotSameTypeAs;
use static_assertions::{assert_impl_all, assert_not_impl_any};

struct A(i32);
struct B(i32);

assert_impl_all!(A: NotSameTypeAs<B>);
assert_not_impl_any!(A: NotSameTypeAs<A>);

Type aliases should work as expected:

use spidermeme::NotSameTypeAs;
use static_assertions::{assert_impl_all, assert_not_impl_any};

type AliasOfI32 = i32;

assert_impl_all!(AliasOfI32: NotSameTypeAs<i64>);
assert_not_impl_any!(AliasOfI32: NotSameTypeAs<i32>);

Generics should work too:

use spidermeme::NotSameTypeAs;
use static_assertions::{assert_impl_all, assert_not_impl_any};
use std::marker::PhantomData;

struct Generic<T>(PhantomData<T>);

assert_impl_all!(Generic<i32>: NotSameTypeAs<Generic<f64>>);
assert_not_impl_any!(Generic<i32>: NotSameTypeAs<Generic<i32>>);

Different kinds of references should work too:

use spidermeme::NotSameTypeAs;

struct Pair<T1, T2>(T1, T2);

trait Homogeneous {
    fn is_same(&self) -> bool;
}
impl<T1, T2> Homogeneous for Pair<T1, T2> {
    fn is_same(&self) -> bool { true }
}
impl<T1, T2> Pair<T1, T2> where T1: NotSameTypeAs<T2> {
    fn is_same(&self) -> bool { false }
}

let x: i32 = 1;
let mut y: i32 = 1;
let same = Pair(&x, &x);
let different = Pair(&x, &mut y);

assert!(same.is_same());
assert!(!different.is_same());

Different lifetimes don’t make them different. The following two examples do not compile:

use spidermeme::NotSameTypeAs;

struct Generic<'a, 'b>(&'a(), &'b()) where &'a(): NotSameTypeAs<&'b()>;

let x = ();
let y = ();
let z = Generic(&x, &y);
use spidermeme::NotSameTypeAs;

struct Generic<'a, 'b>(&'a str, &'b str) where &'a str: NotSameTypeAs<&'b str>;

let x: &'static str = "x";
let y = String::from("y");
let z = Generic(x, &y);

Function pointers should work:

use spidermeme::NotSameTypeAs;
use static_assertions::{assert_impl_all, assert_not_impl_any};

assert_impl_all!(fn(i32) -> i32: NotSameTypeAs<fn(i32) -> i64>);
assert_not_impl_any!(fn(i32) -> i32: NotSameTypeAs<fn(i32) -> i32>);

Closures should also work:

use spidermeme::NotSameTypeAs;

struct Pair<T1, T2>(T1, T2);

trait Homogeneous {
    fn is_same(&self) -> bool;
}
impl<T1, T2> Homogeneous for Pair<T1, T2> {
    fn is_same(&self) -> bool { true }
}
impl<T1, T2> Pair<T1, T2> where T1: NotSameTypeAs<T2> {
    fn is_same(&self) -> bool { false }
}

let x = || 1;
let y = || 1;
let same = Pair(x, x);
let different = Pair(x, y);

assert!(same.is_same());
assert!(!different.is_same());

Implementors

impl<T1, T2> NotSameTypeAs<T1> for T2 where
    TypePair<T1, T2>: DifferentTypes, 
[src]

Loading content...