Skip to main content

ShrinkMut

Trait ShrinkMut 

Source
pub trait ShrinkMut: AllocMut + DeallocMut {
    // Provided method
    unsafe fn shrink_mut(
        &mut self,
        ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<NonNull<u8>, <Self as AllocErrorType>::Error> { ... }
}
Expand description

A memory allocation interface which may require mutable access to itself and can also shrink allocations.

All types which are Shrink are also ShrinkMut, making this more generic than Shrink.

Provided Methods§

Source

unsafe fn shrink_mut( &mut self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, <Self as AllocErrorType>::Error>

Shrinks the given block to a new, smaller layout.

On failure, the original memory will not be deallocated.

Note that the default implementation simply:

  1. Checks that the new layout is smaller or the same size. If both layouts are the same, ptr is returned and no operation is performed.
  2. Allocates a new block of memory via AllocMut::alloc_mut.
  3. Copies new_layout.size() bytes from the old block to the new block. This will discard any extra bytes from the old block.
  4. Deallocates the old block.
  5. Returns a pointer to the new block.
§Safety

The caller must ensure:

  • ptr points to a block of memory allocated using this allocator.
  • old_layout describes exactly the same block.
§Errors

Errors are implementation-defined, refer to AllocErrorType::Error and Error.

The standard implementations may return:

Implementors§

Source§

impl<A: Shrink + ?Sized> ShrinkMut for A