[][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> Copy for Id<S, T>[src]

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

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

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

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

fn max(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Returns max if self is greater than max, and min if self is less than min. Otherwise this will return self. Panics if min > max. Read more

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

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0
[src]

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

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0
[src]

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

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0
[src]

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

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0
[src]

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

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

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

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

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

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

Auto Trait Implementations

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

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

Blanket Implementations

impl<T> From for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

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

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

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

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