/// Places a `value` into the specified `mem` buffer. The user should provide enough memory
/// to store the value with its metadata considering alignment requirements.
///
/// # Panics
///
/// - If the provided buffer is insufficient to store the value.
Places a `value` into the specified `mem` buffer. The user should provide enough memory
to store the value with its metadata considering alignment requirements.
Places a ` value ` into the specified ` mem ` buffer . The user should provide enough memory to store the value with its metadata considering alignment requirements .
pub fn new<Value>(mem: &'m mut [u8], value: Value) -> Self
    where
        Value: Unsize<T>,
    {
        let (meta, layout, offset) = meta_offset_layout(&value);
        assert!(layout.size() > 0, "Unsupported value layot");
        // Construct a box to move the specified memory into the necessary location.
        let mut new_box = Self {
            align_offset: 0,
            mem,
            phantom: PhantomData,
        };
        let raw_ptr = new_box.mem.as_mut().as_mut_ptr();
        // Compute the offset that needs to be applied to the pointer in order to make
        // it aligned correctly.
        new_box.align_offset = raw_ptr.align_offset(layout.align());
        let total_len = new_box.align_offset + layout.size();
        let buf_len = new_box.mem.as_ref().len();
        // Check that the provided buffer has sufficient capacity to store the given value.
        if total_len > buf_len {
            // At the moment we cannot rely on the regular drop implementation because
            // the box is in an inconsistent state.
            core::mem::forget(new_box);
            panic!(
                "Not enough memory to store the specified value (got: {}, needed: {})",
                buf_len, total_len,
            );
        }
        unsafe {
            let ptr = NonNull::new(raw_ptr.add(new_box.align_offset)).unwrap();
            // Store dynamic metadata at the beginning of the given memory buffer.
            ptr.cast::<DynMetadata<T>>().as_ptr().write(meta);
            // Store the value in the remainder of the memory buffer.
            ptr.cast::<u8>()
                .as_ptr()
                .add(offset)
                .cast::<Value>()
                .write(value);
            new_box
        }
    }
pub fn new < Value > ( mem : & ' m mut [ u8 ] , value : Value ) -> Self where Value : Unsize < T > , { let ( meta , layout , offset ) = meta_offset_layout ( & value ) ; assert ! ( layout size ( ) 0 " " ) ; // Construct a box to move the specified memory into the necessary location. let mut new_box = Self { align_offset : 0 , mem , phantom : PhantomData , } ; let raw_ptr = new_box . mem . as_mut ( ) . as_mut_ptr ( ) ; // Compute the offset that needs to be applied to the pointer in order to make // it aligned correctly. new_box . align_offset = raw_ptr . align_offset ( layout . align ( ) ) ; let total_len = new_box . align_offset + layout . size ( ) ; let buf_len = new_box . mem . as_ref ( ) . len ( ) ; // Check that the provided buffer has sufficient capacity to store the given value. if total_len > buf_len { // At the moment we cannot rely on the regular drop implementation because // the box is in an inconsistent state. core :: mem :: forget ( new_box ) ; panic ! ( " " buf_len total_len ) ; } unsafe { let ptr = NonNull :: new ( raw_ptr . add ( new_box . align_offset ) ) . unwrap ( ) ; // Store dynamic metadata at the beginning of the given memory buffer. ptr . cast :: < DynMetadata < T > > ( ) . as_ptr ( ) . write ( meta ) ; // Store the value in the remainder of the memory buffer. ptr . cast :: < u8 > ( ) . as_ptr ( ) . add ( offset ) . cast :: < Value > ( ) . write ( value ) ; new_box } }
Places a `value` into the specified `mem` buffer.
Places a ` value ` into the specified ` mem ` buffer .
mem
&'m mut [u8]
value
Value
panics
If the provided buffer is insufficient to store the value.
If the provided buffer is insufficient to store the value .