[][src]Struct refl::Id

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

A proof term that S and T are the same type (type identity). This type is only ever 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 cannot construct an Id<&'a T, &'b T>. If we could, which we would if we had covariance, we could define the following unsound function in safe Rust:

This example is not 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]

pub const REFL: Self[src]

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

This is equivalent to refl().

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

pub fn cast(self, value: S) -> T where
    S: Sized,
    T: Sized
[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.

pub fn sym(self) -> Id<T, S>[src]

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

pub fn trans<U: ?Sized>(self, _: Id<T, U>) -> Id<S, U>[src]

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

pub fn cast_ref<'a>(self, value: &'a S) -> &'a T[src]

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

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

pub fn cast_ref_mut<'a>(self, value: &'a mut S) -> &'a mut T[src]

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

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: ?Sized, T: ?Sized> Clone for Id<S, T>[src]

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

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

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

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

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

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

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

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

Auto Trait Implementations

impl<S: ?Sized, T: ?Sized> Send for Id<S, T>

impl<S: ?Sized, T: ?Sized> Sync for Id<S, T>

impl<S: ?Sized, T: ?Sized> Unpin for Id<S, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.