Struct Id

Source
pub struct Id<S: ?Sized, T: ?Sized>(/* private fields */);
Expand description

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:

fn transmute_lifetime<'a, 'b, T: 'a + 'b>(r: &'a T) -> &'b T {
    refl().cast(r)
}

See

for more information on variance.

Implementations§

Source§

impl<T: ?Sized> Id<T, T>

Source

pub const REFL: Self

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

This is equivalent to refl().

Source§

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

Source

pub fn cast(self, value: S) -> T
where S: Sized, T: Sized,

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.

Source

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

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

Source

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

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

Source

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

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

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

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§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

fn clone_from(&mut self, _: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<X: ?Sized> Default for Id<X, X>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

fn hash<H: Hasher>(&self, _: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

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

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

fn cmp(&self, _other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

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

Source§

fn eq(&self, _: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, _: &Self) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn partial_cmp(&self, _: &Self) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<S, T> Freeze for Id<S, T>
where S: ?Sized, T: ?Sized,

§

impl<S, T> RefUnwindSafe for Id<S, T>
where S: ?Sized, T: ?Sized,

§

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

§

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

§

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

§

impl<S, T> UnwindSafe for Id<S, T>
where S: ?Sized, T: ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.