Struct refl::Id [] [src]

pub struct Id<S: ?Sized, T: ?Sized>(_);

A proof term that S and T are the same type (type identity). This type is only every inhabited when S is nominally equivalent to T.

A note on variance and safety

S and T are invariant, here, this means that for two, possibly disjoint, lifetimes 'a, 'b we can not construct an Id<&'a T, &'b T>. If we could, which we would if we had covariance, we could define the following unsafe function in safe Rust:

Be careful when using this code, it's not being tested!
fn transmute_lifetime<'a, 'b, T: 'a + 'b>(r: &'a T) -> &'b T {
    refl().cast(r)
}

See

for more information on variance.

Methods

impl<T: ?Sized> Id<T, T>
[src]

REFL: Self = Id(PhantomData)

A proof witness of the fact that a type is equivalent to itself.

The benefit of this version compared to refl() is that this is usable in const contexts while refl() can't.

impl<S: ?Sized, T: ?Sized> Id<S, T>
[src]

[src]

Casts a value of type S to T.

This is safe because the Id type is always guaranteed to only be inhabited by Id<T, T> types by construction.

[src]

Converts Id<S, T> into Id<T, S> since type equality is symmetric.

[src]

If you have proofs Id<S, T> and Id<T, U> you can conclude Id<S, U> since type equality is transitive.

[src]

Casts a value of type &S to &T where S == T.

extern crate refl;
use refl::*;

fn main() {
    let x: Box<u8> = Box::new(1);
    let refl: Id<Box<u8>, Box<u8>> = refl();
    assert_eq!(&x, refl.cast_ref(&x));
}

[src]

Casts a value of type &S to &T where S == T.

extern crate refl;
use refl::*;

fn main() {
    let mut x: Box<u8> = Box::new(1);
    let refl: Id<Box<u8>, Box<u8>> = refl();
    **refl.cast_ref_mut(&mut x) += 1;
    assert_eq!(*x, 2);
}

Trait Implementations

impl<S: Copy + ?Sized, T: Copy + ?Sized> Copy for Id<S, T>
[src]

impl<S: Clone + ?Sized, T: Clone + ?Sized> Clone for Id<S, T>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<S: Debug + ?Sized, T: Debug + ?Sized> Debug for Id<S, T>
[src]

[src]

Formats the value using the given formatter.

impl<S: Hash + ?Sized, T: Hash + ?Sized> Hash for Id<S, T>
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<S: PartialEq + ?Sized, T: PartialEq + ?Sized> PartialEq for Id<S, T>
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl<S: Eq + ?Sized, T: Eq + ?Sized> Eq for Id<S, T>
[src]

impl<S: PartialOrd + ?Sized, T: PartialOrd + ?Sized> PartialOrd for Id<S, T>
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

[src]

This method tests less than (for self and other) and is used by the < operator. Read more

[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<S: Ord + ?Sized, T: Ord + ?Sized> Ord for Id<S, T>
[src]

[src]

This method returns an Ordering between self and other. Read more

1.22.0
[src]

Compares and returns the maximum of two values. Read more

1.22.0
[src]

Compares and returns the minimum of two values. Read more

impl<X: ?Sized> Default for Id<X, X>
[src]

[src]

Returns the "default value" for a type. Read more

impl<A: ?Sized, B: ?Sized> Sync for Id<A, B>
[src]

impl<A: ?Sized, B: ?Sized> Send for Id<A, B>
[src]