BoxBytes is a type that represents a heap allocation of [u8] but preserves the original Layout (alignment and size) of the allocation. This is useful when you want to treat a Box<T> as a byte slice without losing its alignment properties.
Key Operations:
- Convert
Box<T> to BoxBytes: Use box_bytes_of(input: Box<T>) or BoxBytes::from(value). This works for Sized + NoUninit types, slices [T], and str. - Convert
BoxBytes back to Box<T>: Use try_from_box_bytes(input: BoxBytes) -> Result<Box<T>, (PodCastError, BoxBytes)>. This is safe if the alignment and size match. - Unwrap conversion: Use
from_box_bytes<T>(input: BoxBytes) -> Box<T> which panics on error.
Accessing data: BoxBytes implements Deref<Target = [u8]> and DerefMut.
pub struct BoxBytes {
ptr: NonNull<u8>,
layout: Layout,
}
pub fn box_bytes_of<T: sealed::BoxBytesOf + ?Sized>(input: Box<T>) -> BoxBytes
pub fn try_from_box_bytes<T: sealed::FromBoxBytes + ?Sized>(input: BoxBytes) -> Result<Box<T>, (PodCastError, BoxBytes)>
pub fn from_box_bytes<T: sealed::FromBoxBytes + ?Sized>(input: BoxBytes) -> Box<T>