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
- https://doc.rust-lang.org/nightly/nomicon/phantom-data.html
- https://doc.rust-lang.org/beta/nomicon/subtyping.html
for more information on variance.
Implementations§
Source§impl<S: ?Sized, T: ?Sized> Id<S, T>
impl<S: ?Sized, T: ?Sized> Id<S, T>
Sourcepub fn cast(self, value: S) -> T
pub fn cast(self, value: S) -> T
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.
Sourcepub fn trans<U: ?Sized>(self, _: Id<T, U>) -> Id<S, U>
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.
Sourcepub fn cast_ref<'a>(self, value: &'a S) -> &'a T
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));
}Sourcepub fn cast_ref_mut<'a>(self, value: &'a mut S) -> &'a mut T
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> Ord for Id<S, T>
impl<S: ?Sized, T: ?Sized> Ord for Id<S, T>
Source§impl<S: ?Sized, T: ?Sized> PartialOrd for Id<S, T>
impl<S: ?Sized, T: ?Sized> PartialOrd for Id<S, T>
impl<S: ?Sized, T: ?Sized> Copy for Id<S, T>
impl<S: ?Sized, T: ?Sized> Eq for Id<S, T>
Auto Trait Implementations§
impl<S, T> Freeze for Id<S, T>
impl<S, T> RefUnwindSafe for Id<S, T>
impl<S, T> Send for Id<S, T>
impl<S, T> Sync for Id<S, T>
impl<S, T> Unpin for Id<S, T>
impl<S, T> UnwindSafe for Id<S, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more