[−][src]Struct refl::Id
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
- https://doc.rust-lang.org/nightly/nomicon/phantom-data.html
- https://doc.rust-lang.org/beta/nomicon/subtyping.html
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]
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.
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]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, _: &Self)
[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]
fn hash<H: Hasher>(&self, _: &mut H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<S: ?Sized, T: ?Sized> Ord for Id<S, T>
[src]
fn cmp(&self, _other: &Self) -> Ordering
[src]
#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
[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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,