Struct singleton_trait::Erased [−][src]
pub struct Erased<T> { /* fields omitted */ }Expand description
The Erased struct witnesses the logical ownership of a value of type T while remaining zero-sized. This can be used for ghost proofs of soundness.
Erased
NOTE: drop implementations will never be called, as Exists
Secondly, keep in mind that while Erased&T from an Exists<&T> and *mut T or &UnsafeCell<T>
even when T is Singleton, but this could be possible if T is zero-sized.
Because of the missing provenance, creating a reference this way could invalidate
the original reference on which this Exists instance was based.
Implementations
This function constructs a value of Erased
Safety
Constructing this asserts that there is a value of type T which has been leaked, or in which it is guaranteed that the program behaves the same up to observation as if a zero-sized copy of T were being passed.
Turns a &Erased
This can be especially useful when storing
the Erased
use core::cell::RefCell;
struct Token;
let lock = RefCell::new(Erased::new(Token));
let locked = lock.borrow();
let _: Erased<&Token> = locked.borrow();Turns a &mut Erased
This can be especially useful when storing
the Erased
use core::cell::RefCell;
struct Token;
let lock = RefCell::new(Erased::new(Token));
let mut locked = lock.borrow_mut();
let _: Erased<&mut Token> = locked.borrow_mut();Maps a function on the inside of the Erased field.
Safety
Due to the strictness guarantees, the passed closure must not cause any visible side effects, including side effects caused by owning R
pub unsafe fn map_borrow<'a, R, F: FnOnce(&'a T) -> R>(
&'a self,
_: impl Exists<F>
) -> Erased<R>
pub unsafe fn map_borrow<'a, R, F: FnOnce(&'a T) -> R>(
&'a self,
_: impl Exists<F>
) -> Erased<R>
Maps a function on the borrow of the Erased field.
Safety
Due to the strictness guarantees, the passed closure must not cause any visible side effects, including side effects caused by owning R
pub unsafe fn map_borrow_mut<'a, R, F: FnOnce(&'a mut T) -> R>(
&'a mut self,
_: impl Exists<F>
) -> Erased<R>
pub unsafe fn map_borrow_mut<'a, R, F: FnOnce(&'a mut T) -> R>(
&'a mut self,
_: impl Exists<F>
) -> Erased<R>
Maps a function on the mutable borrow of the Erased field.
Safety
Due to the strictness guarantees, the passed closure must not cause any visible side effects, including side effects caused by owning R
Fallback function which converts this Erased value into an Exists implementer.
This exists because we cannot allow general trait implementations due to coherence rules, but we can’t be sufficiently flexible in trait implementations due to a lack of subtyping constraints or trait covariance, which would mean references would be too inflexible
Re-borrow this erased mutable borrow. Usually this is implicit in Rust, but not for general wrappers. This can be seen as a “clone” method for mutable borrows, as it allows you to retain the erased borrow after the end of the lifetime.
The Exists trait can perform this conversion automatically.
use singleton_trait::Erased;
struct Token; fn recursively(mut b: Erased<&mut Token>) { recursively(b.reborrow()); recursively(b); }
Borrow this erased mutable borrow as immutable. This is effectively a Deref method under Erased
The Exists trait can perform this conversion automatically.
struct Lock;
fn use_lock(write: Erased<&mut Lock>) {
read_from(write.read());
}
fn read_from(read: Erased<&Lock>) { }Consume this erased mutable borrow to turn it into an immutable borrow.
The Exists trait can perform this conversion automatically.
See also read, which is often more useful.
This method uses the full inner lifetime
without needing to borrow.
struct Lock;
struct MutWrapper<'a> (Erased<&'a mut Lock>);
struct Wrapper<'a> (Erased<&'a Lock>);
impl<'a> MutWrapper<'a> {
pub fn into_read(self) -> Wrapper<'a> {
Wrapper(self.0.into_shared())
}
}