//! Implement Fallible Box usesuper::TryClone; usecrate::TryReserveError; use alloc::boxed::Box; use core::borrow::Borrow; use core::mem::ManuallyDrop; use core::ops::Deref;
/// trait to implement Fallible Box pubtrait FallibleBox<T> { /// try creating a new box, returning a Result<Box<T>, /// TryReserveError> if allocation failed fn try_new(t: T) -> Result<Self, TryReserveError> where Self: Sized;
} /// TryBox is a thin wrapper around alloc::boxed::Box to provide support for /// fallible allocation. /// /// See the crate documentation for more. pubstruct TryBox<T> {
inner: Box<T>,
}
impl<T> FallibleBox<T> forBox<T> { fn try_new(t: T) -> Result<Self, TryReserveError> { letmut vec = alloc::vec::Vec::new();
vec.try_reserve_exact(1)?;
vec.push(t); // try_reserve_exact doesn't promise the exact size, but into_boxed_slice does. // in practice the size is going to be okay anyway, so it won't realloc. let ptr: *mut T = ManuallyDrop::new(vec.into_boxed_slice()).as_mut_ptr();
Ok(unsafe { Box::from_raw(ptr) })
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.