pub struct Box<T, A>where
A: Allocator,{ /* private fields */ }Expand description
A Müsli-allocated pointer type that uniquely owns a heap allocation of type
T.
This is a Box type capable of using the allocator provided
through a Context. Therefore it can be safely used in no-std
environments.
§Thread safety
A box is Send or Sync only if the allocation it holds is. Boxes
backed by the global allocator are thread safe as long as T is:
use musli::alloc::{Box, Global};
fn assert_send_sync<T: Send + Sync>(_: &T) {}
let boxed = Box::<u32, Global>::new_in(42, Global::new())?;
assert_send_sync(&boxed);But a box allocated from a Slice refers to allocator state which is not
synchronized, so it cannot be sent to another thread:
ⓘ
use musli::alloc::{ArrayBuffer, Box, Slice};
fn assert_send<T: Send>(_: &T) {}
let mut buf = ArrayBuffer::new();
let alloc = Slice::new(&mut buf);
let boxed = Box::new_in(42u32, &alloc)?;
assert_send(&boxed);Nor can it be shared between threads:
ⓘ
use musli::alloc::{ArrayBuffer, Box, Slice};
fn assert_sync<T: Sync>(_: &T) {}
let mut buf = ArrayBuffer::new();
let alloc = Slice::new(&mut buf);
let boxed = Box::new_in(42u32, &alloc)?;
assert_sync(&boxed);Implementations§
Source§impl<T, A> Box<T, A>where
A: Allocator,
impl<T, A> Box<T, A>where
A: Allocator,
Sourcepub fn new_in(value: T, alloc: A) -> Result<Self, AllocError>
pub fn new_in(value: T, alloc: A) -> Result<Self, AllocError>
Allocates memory on the heap and then places x into it.
This doesn’t actually allocate if T is zero-sized.
§Examples
use musli::alloc::{AllocError, Box};
musli::alloc::default(|alloc| {
let mut a = Box::new_in(10u32, alloc)?;
assert_eq!(a.as_ref(), &10u32);
Ok::<_, AllocError>(())
});Zero-sized types:
use musli::alloc::{AllocError, Box};
musli::alloc::default(|alloc| {
let mut a = Box::new_in((), alloc)?;
assert_eq!(a.as_ref(), &());
Ok::<_, AllocError>(())
});Trait Implementations§
impl<T, A> Eq for Box<T, A>
Source§impl<T, A> Hasher for Box<T, A>
impl<T, A> Hasher for Box<T, A>
Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
Writes a single
u128 into this hasher.Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
Writes a single
usize into this hasher.Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
Writes a single
i128 into this hasher.Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
Writes a single
isize into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
🔬This is a nightly-only experimental API. (
hasher_prefixfree_extras)Writes a length prefix into this hasher, as part of being prefix-free. Read more
Source§impl<T, A> Ord for Box<T, A>
impl<T, A> Ord for Box<T, A>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Auto Trait Implementations§
impl<T, A> Freeze for Box<T, A>
impl<T, A> RefUnwindSafe for Box<T, A>where
<A as Allocator>::Alloc<T>: RefUnwindSafe,
impl<T, A> Send for Box<T, A>
impl<T, A> Sync for Box<T, A>
impl<T, A> Unpin for Box<T, A>
impl<T, A> UnsafeUnpin for Box<T, A>where
<A as Allocator>::Alloc<T>: UnsafeUnpin,
impl<T, A> UnwindSafe for Box<T, A>where
<A as Allocator>::Alloc<T>: UnwindSafe,
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
Mutably borrows from an owned value. Read more