use super::*;
use crate::AllocatedShape;
impl<'facet> Partial<'facet, true> {
pub fn alloc<T>() -> Result<Self, ReflectError>
where
T: Facet<'facet> + ?Sized,
{
unsafe { Self::alloc_shape(T::SHAPE) }
}
pub unsafe fn alloc_shape(shape: &'static Shape) -> Result<Self, ReflectError> {
alloc_shape_inner(shape)
}
}
impl Partial<'static, false> {
pub fn alloc_owned<T>() -> Result<Self, ReflectError>
where
T: Facet<'static> + ?Sized,
{
unsafe { Self::alloc_shape_owned(T::SHAPE) }
}
pub unsafe fn alloc_shape_owned(shape: &'static Shape) -> Result<Self, ReflectError> {
alloc_shape_inner(shape)
}
}
fn alloc_shape_inner<'facet, const BORROW: bool>(
shape: &'static Shape,
) -> Result<Partial<'facet, BORROW>, ReflectError> {
crate::trace!(
"alloc_shape({:?}), with layout {:?}",
shape,
shape.layout.sized_layout()
);
let data = shape.allocate().map_err(|_| ReflectError::Unsized {
shape,
operation: "alloc_shape",
})?;
let allocated_size = shape.layout.sized_layout().expect("must be sized").size();
let mut stack = Vec::with_capacity(4);
stack.push(Frame::new(
data,
AllocatedShape::new(shape, allocated_size),
FrameOwnership::Owned,
));
Ok(Partial {
mode: FrameMode::Strict { stack },
state: PartialState::Active,
invariant: PhantomData,
})
}