Struct manually_boxed::Borrowed

source ·
pub struct Borrowed<'t, T: ?Sized> { /* private fields */ }
Expand description

A Ptr borrowed from stack data

Implementations§

source§

impl<'t, T: ?Sized> Borrowed<'t, T>

source

pub unsafe fn new(t: &mut T) -> Self

Obtain a Ptr from borrowed data

Derefs to Ptr<T>.

Call .retained() at the point(s) where it is OK for the Ptrs to become invalid.

§SAFETY

You must ensure that the Ptr (and all its copies) is only used while the Borrowed still exists.

The lifetime cannot be completely checked by the compiler, and Rust’s drop behaviour can cause the Ptr be invalidated (for example, by the underlying T being dropped, or reborrowed) earlier than you might expect.

Using retained is recommended: it will ensure that Ptrs are still live at that point.

§Unwinding

Not that Borrowed comes with exciting hazards in the presence of panics: a typical use is to take a stack item, link it temporarily into a data structure.

If an unwind occurs before the item is unlinked again, the data structure can be corrupted. It is the responsibility of the user to avoid this situation.

(Note that use of Ptr without Borrowed does also come with hazards in the face of unwinding, but those are typically just memory leaks.)

source

pub fn retained(&self)

Prove that a Borrowed is still live.

This function is a no-op, in itself.

But calling it assures that the lifetime of the Borrow extends at least this far.

Trait Implementations§

source§

impl<'t, T> Deref for Borrowed<'t, T>

§

type Target = Ptr<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Ptr<T>

Dereferences the value.

Auto Trait Implementations§

§

impl<'t, T> Freeze for Borrowed<'t, T>
where T: ?Sized,

§

impl<'t, T> RefUnwindSafe for Borrowed<'t, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'t, T> Send for Borrowed<'t, T>
where T: Send + Sync + ?Sized,

§

impl<'t, T> Sync for Borrowed<'t, T>
where T: Sync + ?Sized,

§

impl<'t, T> Unpin for Borrowed<'t, T>
where T: ?Sized,

§

impl<'t, T> UnwindSafe for Borrowed<'t, T>
where T: RefUnwindSafe + ?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> 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>,

§

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>,

§

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.