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

source

pub fn new_heap(t: T) -> Self
where T: Sized,

Makes a new Ptr, moving the T to a new heap allocation

source

pub fn borrow<'a>(self, tok: impl IsTokenRef<'a>) -> &'a T

Borrows the contained data, immutably

source

pub fn borrow_mut<'a>(self, tok: impl IsTokenMut<'a>) -> &'a mut T

Borrows the contained data, mutably

source

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().

source

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.

source

pub unsafe fn free_heap_return<'a>(self, tok: impl IsTokenMut<'a>) -> T
where T: Sized,

Frees a Ptr that was made with new_heap and returns the T

If T is not Sized, you must use free_heap (discarding T) or into_box (leaving it on the heap).

§SAFETY

The same rules as free_heap apply.

source

pub unsafe fn into_box<'a>(self, tok: impl IsTokenMut<'a>) -> Box<T>

Converts a Ptr that was made with new_heap into a Box<T>

§SAFETY

The same rules as free_heap apply.

source

pub unsafe fn from_raw(t: NonNull<T>) -> Self

Make a new Ptr out of a raw pointer

§SAFETY

It is up to you to ensure that the pointer is valid for T, dereferencable, and has appropriate lifespan.

Trait Implementations§

source§

impl<T: ?Sized> Clone for Ptr<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: ?Sized> Debug for Ptr<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized> From<Box<T>> for Ptr<T>

source§

fn from(b: Box<T>) -> Ptr<T>

Converts to this type from the input type.
source§

impl<T: ?Sized> Copy for Ptr<T>

source§

impl<T: Send + ?Sized> Send for Ptr<T>

source§

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> 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> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.