Struct static_alloc::leaked::LeakBox [−][src]
Represents an allocation within a Bump.
This is an owning pointer comparable to Box. It drops the contained value when it is dropped
itself. The difference is that no deallocation logic is ever executed.
Usage
This box can be used to manage one valid instance constructed within the memory provided by a
MaybeUninit instance.
use core::mem::MaybeUninit; use static_alloc::leaked::LeakBox; let mut storage = MaybeUninit::uninit(); let leak_box = LeakBox::from(&mut storage); // The string itself is not managed by `static_alloc`. let mut instance = LeakBox::write(leak_box, String::new()); instance.push_str("Hello world!");
This box is the result of allocating from one of the Bump allocators using its explicit API.
Being a box-like type, an Option has the same size.
use core::mem::size_of; use static_alloc::leaked::LeakBox; type Boxed = LeakBox<'static, usize>; type Optional = Option<Boxed>; assert_eq!(size_of::<Boxed>(), size_of::<Optional>());
TODO: On nightly the inner type should be unsizable.
Implementations
impl<'ctx, T: ?Sized> LeakBox<'ctx, T>[src]
pub fn into_raw(this: Self) -> *mut T[src]
Retrieve the raw pointer wrapped by this box.
After this method the caller is responsible for managing the value in the place behind the pointer. It will need to be dropped manually.
Usage
You might manually drop the contained instance at a later point.
use static_alloc::{Bump, leaked::LeakBox}; let bump: Bump<[usize; 128]> = Bump::uninit(); let leak_box = bump.leak_box(String::from("Hello"))?; let ptr = LeakBox::into_raw(leak_box); unsafe { core::ptr::drop_in_place(ptr); }
An alternative is to later re-wrap the pointer
use static_alloc::{Bump, leaked::LeakBox}; let bump: Bump<[usize; 128]> = Bump::uninit(); let leak_box = bump.leak_box(String::from("Hello"))?; let ptr = LeakBox::into_raw(leak_box); unsafe { let _ = LeakBox::from_raw(ptr); };
pub unsafe fn from_raw(pointer: *mut T) -> Self[src]
Wrap a raw pointer.
The most immediate use is to rewrap a pointer returned from into_raw.
Safety
The pointer must point to a valid instance of T that is not aliased by any other
reference for the lifetime 'ctx. In particular it must be valid aligned and initialized.
Dropping this LeakBox will drop the instance, which the caller must also guarantee to be
sound.
pub fn leak<'a>(this: Self) -> &'a mut T where
'ctx: 'a, [src]
'ctx: 'a,
Leak the instances as a mutable reference.
After calling this method the value is no longer managed by LeakBox. Its Drop impl will
not be automatically called.
Usage
use static_alloc::{Bump, leaked::LeakBox}; let bump: Bump<[usize; 128]> = Bump::uninit(); let leak_box = bump.leak_box(String::from("Hello"))?; let st: &mut String = LeakBox::leak(leak_box);
You can't leak past the lifetime of the allocator.
let bump: Bump<[usize; 128]> = Bump::uninit(); let leak_box = bump.leak_box(String::from("Hello"))?; let st: &mut String = LeakBox::leak(leak_box); drop(bump); // error[E0505]: cannot move out of `bump` because it is borrowed st.to_lowercase(); //-- borrow later used here
impl<'ctx, T> LeakBox<'ctx, T>[src]
pub fn take(this: Self) -> T[src]
Remove the value, forgetting the box in the process.
This is similar to dereferencing a box (*leak_box) but no deallocation is involved. This
becomes useful when the allocator turns out to have too short of a lifetime.
Usage
You may want to move a long-lived value out of the current scope where it's been allocated.
use static_alloc::{Bump, leaked::LeakBox}; let cell = RefCell::new(0usize); let guard = { let bump: Bump<[usize; 128]> = Bump::uninit(); let mut leaked = bump.leak_box(cell.borrow_mut()).unwrap(); **leaked = 1usize; // Take the value, allowing use independent of the lifetime of bump LeakBox::take(leaked) }; assert!(cell.try_borrow().is_err()); drop(guard); assert!(cell.try_borrow().is_ok());
pub fn from_mut(val: &'ctx mut T) -> Self where
T: Copy, [src]
T: Copy,
Wrap a mutable reference to a trivial value as if it were a box.
This is safe because such values can not have any Drop code and can be duplicated at will.
The usefulness of this operation is questionable but the author would be delighted to hear about any actual use case.
impl<'ctx, T> LeakBox<'ctx, MaybeUninit<T>>[src]
pub fn write(mut this: Self, val: T) -> LeakBox<'ctx, T>[src]
Write a value into this box, initializing it.
This can be used to delay the computation of a value until after an allocation succeeded while maintaining all types necessary for a safe initialization.
Usage
use static_alloc::{Bump, leaked::LeakBox}; let bump: Bump<[usize; 128]> = Bump::uninit(); let memory = bump.leak_box(MaybeUninit::uninit())?; let value = LeakBox::write(memory, some_expensive_operation());
pub unsafe fn assume_init(this: Self) -> LeakBox<'ctx, T>[src]
Converts to LeakBox<T>.
Safety
The value must have been initialized as required by MaybeUninit::assume_init. Calling
this when the content is not yet fully initialized causes immediate undefined behavior.
Trait Implementations
impl<T: ?Sized> AsMut<T> for LeakBox<'_, T>[src]
impl<T: ?Sized> AsRef<T> for LeakBox<'_, T>[src]
impl<T: Debug + ?Sized> Debug for LeakBox<'_, T>[src]
impl<'ctx, T: ?Sized> Deref for LeakBox<'ctx, T>[src]
impl<'ctx, T: ?Sized> DerefMut for LeakBox<'ctx, T>[src]
impl<T: Display + ?Sized> Display for LeakBox<'_, T>[src]
impl<T: ?Sized> Drop for LeakBox<'_, T>[src]
impl<'ctx, T> From<&'ctx mut [MaybeUninit<T>]> for LeakBox<'ctx, [MaybeUninit<T>]>[src]
Construct a LeakBox to an existing slice of MaybeUninit.
fn from(uninit: &'ctx mut [MaybeUninit<T>]) -> Self[src]
impl<'ctx, T> From<&'ctx mut MaybeUninit<T>> for LeakBox<'ctx, MaybeUninit<T>>[src]
Construct a LeakBox to an existing MaybeUninit.
The MaybeUninit type is special in that we can treat any unique reference to an owned value as an owned value itself since it has no representational invariants.
fn from(uninit: &'ctx mut MaybeUninit<T>) -> Self[src]
impl<T: Hash + ?Sized> Hash for LeakBox<'_, T>[src]
fn hash<H: Hasher>(&self, h: &mut H)[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
impl<T: ?Sized> Pointer for LeakBox<'_, T>[src]
Auto Trait Implementations
impl<'ctx, T> !Send for LeakBox<'ctx, T>[src]
impl<'ctx, T> !Sync for LeakBox<'ctx, T>[src]
impl<'ctx, T: ?Sized> Unpin for LeakBox<'ctx, T>[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized, [src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,