RecycleBox

Struct RecycleBox 

Source
pub struct RecycleBox<T>
where T: ?Sized,
{ /* private fields */ }
Expand description

A pointer type for heap-allocated objects which heap storage can be re-used.

See the module-level documentation for more.

Implementations§

Source§

impl<T> RecycleBox<T>

Source

pub fn new(x: T) -> Self

Allocates heap memory based on the size of T and places x into it.

Source

pub fn with_layout(x: T, layout: Layout) -> Self

Allocates heap memory based on the specified layout and places x into it.

If type T does not fit within the specified layout, the layout alignment and size are increased as needed.

Source

pub fn replace<U>(boxed: Self, x: U) -> (T, RecycleBox<U>)

Consumes the box and return both the old value and a new box, re-using if possible the already allocated memory.

No memory is allocated unless the new object does not fit within the already allocated memory block.

Source

pub fn take(boxed: Self) -> (T, RecycleBox<()>)

Consumes the box and return both the old value and a box containing an empty tuple.

This is functionally equivalent to calling RecycleBox::replace with an empty tuple as argument, but is more explicit when the intent is specifically to take the contained object while preserving the allocated memory for further re-use. It may also be slightly more efficient.

Source

pub fn pin(x: T) -> Pin<RecycleBox<T>>

Constructs a new Pin<RecycleBox<T>>. If T does not implement Unpin, then x will be pinned in memory and unable to be moved.

Source§

impl<T> RecycleBox<T>
where T: ?Sized,

Source

pub fn recycle<U>(boxed: Self, x: U) -> RecycleBox<U>

Consumes the box and creates another one, re-using if possible the already allocated memory.

The current boxed object is dropped. No memory is allocated unless the new object does not fit within the already allocated memory block.

Source

pub fn vacate(boxed: Self) -> RecycleBox<()>

Consumes the box and creates another one containing an empty tuple.

This is functionally equivalent to calling RecycleBox::recycle with an empty tuple as argument, but is more explicit when the intent is specifically to drop the contained object while preserving the allocated memory for further re-use. It may also be slightly more efficient.

Source

pub fn recycle_pinned<U>(boxed: Pin<RecycleBox<T>>, x: U) -> RecycleBox<U>

Consumes a pinned box and creates another box, re-using if possible the already allocated memory.

This is the same as RecycleBox::recycle but for a pinned RecycleBox. The Pin contract is upheld since the current object is dropped before it is replaced by the new object.

§Example
use std::pin::Pin;
use recycle_box::RecycleBox;

let pinned_box: Pin<_> = RecycleBox::new(42).into();
let new_box = RecycleBox::recycle_pinned(pinned_box, "Forty two");
Source

pub fn vacate_pinned(boxed: Pin<RecycleBox<T>>) -> RecycleBox<()>

Consumes a pinned box and creates another box containing an empty tuple.

This is the same as RecycleBox::vacate but for a pinned RecycleBox. The Pin contract is upheld since the current object is dropped before it is replaced by an empty tuple.

§Example
use std::pin::Pin;
use recycle_box::RecycleBox;

let pinned_box: Pin<_> = RecycleBox::new(42).into();
let empty_box = RecycleBox::vacate_pinned(pinned_box);
Source

pub fn into_pin(boxed: Self) -> Pin<Self>

Converts a RecycleBox<T> into a Pin<RecycleBox<T>>. If T does not implement Unpin, then *boxed will be pinned in memory and unable to be moved.

This conversion does not allocate on the heap and happens in place.

Source

pub unsafe fn from_raw_parts( ptr: *mut T, base_ptr: *mut u8, layout: Layout, ) -> Self

Constructs a box from raw pointers and a layout.

The T object pointed to and the storage defined by the base pointer and the layout become owned by the resulting RecycleBox.

§Safety

The caller is responsible for making sure that the object pointed to exists, is of the proper type and is within an already allocated memory block consistent with the specified base pointer and layout. Also, the user must ensure that ownership of the object and of the allocated memory is exclusively held by the box.

Source

pub fn into_raw_parts(boxed: Self) -> (*mut T, *mut u8, Layout)

Consumes the Box, returning its internal data.

The caller becomes responsible for dropping the object pointed to and deallocating the backing storage.

Trait Implementations§

Source§

impl<T> AsMut<T> for RecycleBox<T>
where T: ?Sized,

Source§

fn as_mut(&mut self) -> &mut T

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<T> for RecycleBox<T>
where T: ?Sized,

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Debug for RecycleBox<T>
where T: Debug + ?Sized,

Source§

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

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

impl<T> Deref for RecycleBox<T>
where T: ?Sized,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for RecycleBox<T>
where T: ?Sized,

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T> Display for RecycleBox<T>
where T: Display + ?Sized,

Source§

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

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

impl<T> Drop for RecycleBox<T>
where T: ?Sized,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<RecycleBox<T>> for Pin<RecycleBox<T>>
where T: ?Sized,

Source§

fn from(boxed: RecycleBox<T>) -> Self

Converts to this type from the input type.
Source§

impl<F> Future for RecycleBox<F>
where F: ?Sized + Future + Unpin,

Source§

type Output = <F as Future>::Output

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl<T> Pointer for RecycleBox<T>
where T: ?Sized,

Source§

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

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

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

Source§

impl<T: Sync + ?Sized> Sync for RecycleBox<T>

Auto Trait Implementations§

§

impl<T> Freeze for RecycleBox<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for RecycleBox<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for RecycleBox<T>
where T: ?Sized,

§

impl<T> UnwindSafe for RecycleBox<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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.