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:
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]
const REFL: Self
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]
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.
fn sym(self) -> Id<T, S>
[src]
Converts Id<S, T>
into Id<T, S>
since type equality is symmetric.
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.
fn cast_ref<'a>(self, value: &'a S) -> &'a T
[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)); }
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
.
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]
fn clone(&self) -> Id<S, T>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<S: Debug + ?Sized, T: Debug + ?Sized> Debug for Id<S, T>
[src]
impl<S: Hash + ?Sized, T: Hash + ?Sized> Hash for Id<S, T>
[src]
fn hash<__HST: Hasher>(&self, __arg_0: &mut __HST)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
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]
fn eq(&self, __arg_0: &Id<S, T>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Id<S, T>) -> bool
[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]
fn partial_cmp(&self, __arg_0: &Id<S, T>) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Id<S, T>) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Id<S, T>) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &Id<S, T>) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Id<S, T>) -> bool
[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]
fn cmp(&self, __arg_0: &Id<S, T>) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.22.0[src]
Compares and returns the minimum of two values. Read more