#owner #equality #traits #equals #child #20041

type-equals

rust-lang/rust#20041 的类型等于特质

1 个不稳定版本

0.1.0 2020年2月1日

#25#owner

无许可协议

5KB

type-equals

crates.io crates.io docs.rs GitHub

一个用于检查类型相等的特质。

此包实现了在 rust-lang/rust#20041 中描述的 TypeEquals 技巧。

以下代码可以编译:

use type_equals::TypeEquals;

pub trait Owner {
    type Child1: Child;
    type Child2: Child;
}

pub trait Child {
    type Owner: Owner;
}

pub struct A;
impl Owner for A {
    type Child1 = B;
    type Child2 = C;
}

pub struct B;
impl Child for B {
    type Owner = A;
}

pub struct C;
impl Child for C {
    type Owner = A;
}

pub fn want_child_one<T: Child>()
where
    <T::Owner as Owner>::Child1: TypeEquals<Other = T>,
{}

pub fn want_child_two<T: Child>()
where
    <T::Owner as Owner>::Child2: TypeEquals<Other = T>,
{}

pub fn this_works() {
    want_child_one::<B>();
    want_child_two::<C>();
}

同时,以下代码无法编译:

// A, B, C, want_child_one and want_child_two are declared identically.

pub fn this_does_not_work() {
    want_child_one::<C>();
    want_child_two::<B>();
}

无运行时依赖