Struct manually_boxed::Ptr
source · pub struct Ptr<T: ?Sized> { /* private fields */ }
Expand description
Pointer to manually-memory-managed T
Ptr<T>
usually refers to an item on the heap, like Box<T>
.
It can be made from T
with Ptr::new_heap()
,
or From<Box>
.
It is then freed again with .free_heap()
.
To access the contained data use,
Ptr::borrow()
and
borrow_mut()
.
To mutably access the data of more than one node at once,
use
IsTokenMut::multi_static
or
IsTokenMut::multi_dynamic
.
To avoid runtime aliasing checks, and take manual control of aliasing,
you can
.borrow_mut()
with
TokenMut::new_unchecked()
;
or,
p
.as_ptr()
.as_ref()
/
p
.as_ptr()
.as_mut()
.
This type is Copy
. Copying it (or, indeed, cloning it)
does not copy the underlying data.
Nor is there any reference counting or garbage collection.
Ensuring that Ptr
s are freed at the right time is up to the caller,
But aliasing, and other Rust unsafe footguns,
are (largely) handled by the library.
See the module-level documentation.
Implementations§
source§impl<T: ?Sized> Ptr<T>
impl<T: ?Sized> Ptr<T>
sourcepub fn new_heap(t: T) -> Selfwhere
T: Sized,
pub fn new_heap(t: T) -> Selfwhere
T: Sized,
Makes a new Ptr
, moving the T
to a new heap allocation
sourcepub fn borrow<'a>(self, tok: impl IsTokenRef<'a>) -> &'a T
pub fn borrow<'a>(self, tok: impl IsTokenRef<'a>) -> &'a T
Borrows the contained data, immutably
sourcepub fn borrow_mut<'a>(self, tok: impl IsTokenMut<'a>) -> &'a mut T
pub fn borrow_mut<'a>(self, tok: impl IsTokenMut<'a>) -> &'a mut T
Borrows the contained data, mutably
sourcepub fn as_ptr(self) -> NonNull<T>
pub fn as_ptr(self) -> NonNull<T>
Returns the raw data pointer
Useful if you want to compare pointers, or something.
If Ptr
was made with new_heap
or From<Box<_>>
,
this pointer has the same properties as Box
’s.
But of course many copies may exist.
§Obtaining references
You may call
.as_ref()
or
.as_mut()
on the returned NonNull
,
to get &T
or &mut T
.
This is sound iff there are no conflicting borrows, and you know you won’t be freeing the data too soon. If you do this, you take responsibility for following Rust’s aliasing rules.
This approach is semantically equivalent to using
TokenMut::new_unchecked()
.
sourcepub unsafe fn free_heap<'a>(self, tok: impl IsTokenMut<'a>)
pub unsafe fn free_heap<'a>(self, tok: impl IsTokenMut<'a>)
Frees a Ptr
that was made with new_heap
The contained T
is dropped.
To keep it, use .free_heap_return()
.
§SAFETY
self
must have come from new_heap
(or From<Box>
)
and be the only remaining copy of this Ptr
(or the only one which will be used).
All copies of self
will be invalidated.
It is your responsibility to ensure
that none of them will be used.
(“used” means passed to any method in this library.)
The compiler will check that no borrows are live.
sourcepub unsafe fn free_heap_return<'a>(self, tok: impl IsTokenMut<'a>) -> Twhere
T: Sized,
pub unsafe fn free_heap_return<'a>(self, tok: impl IsTokenMut<'a>) -> Twhere
T: Sized,
sourcepub unsafe fn into_box<'a>(self, tok: impl IsTokenMut<'a>) -> Box<T>
pub unsafe fn into_box<'a>(self, tok: impl IsTokenMut<'a>) -> Box<T>
Trait Implementations§
impl<T: ?Sized> Copy for Ptr<T>
impl<T: Send + ?Sized> Send for Ptr<T>
impl<T: Sync + ?Sized> Sync for Ptr<T>
Auto Trait Implementations§
impl<T> Freeze for Ptr<T>where
T: ?Sized,
impl<T> RefUnwindSafe for Ptr<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Unpin for Ptr<T>where
T: ?Sized,
impl<T> UnwindSafe for Ptr<T>where
T: RefUnwindSafe + ?Sized,
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
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)