1 unstable release

0.1.0 Feb 1, 2020

#10 in #child

Download history 3/week @ 2024-12-17 4/week @ 2025-01-07 27/week @ 2025-01-14 10/week @ 2025-01-21 7/week @ 2025-02-04 2/week @ 2025-02-11 16/week @ 2025-02-18 8/week @ 2025-02-25 12/week @ 2025-03-04 37/week @ 2025-03-11 21/week @ 2025-03-18 39/week @ 2025-03-25 20/week @ 2025-04-01

117 downloads per month

Unlicense

5KB

type-equals

crates.io crates.io docs.rs GitHub

A trait for checking type equality.

This crate implements the TypeEquals trick described in rust-lang/rust#20041.

The following compiles:

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>();
}

Meanwhile, the following does not compile:

// 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>();
}

No runtime deps