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>
impl<'t, T: ?Sized> Borrowed<'t, T>
sourcepub unsafe fn new(t: &mut T) -> Self
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 Ptr
s 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 Ptr
s 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.)