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
Provided Methods§
Sourceunsafe fn shrink_mut(
&mut self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, <Self as AllocErrorType>::Error>
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:
- Checks that the new layout is smaller or the same size. If both layouts are the same,
ptris returned and no operation is performed. - Allocates a new block of memory via
AllocMut::alloc_mut. - Copies
new_layout.size()bytes from the old block to the new block. This will discard any extra bytes from the old block. - Deallocates the old block.
- Returns a pointer to the new block.
§Safety
The caller must ensure:
ptrpoints to a block of memory allocated using this allocator.old_layoutdescribes exactly the same block.
§Errors
Errors are implementation-defined, refer to AllocErrorType::Error and Error.
The standard implementations may return:
Err(Error::AllocFailed(layout, cause))if allocation fails.causeis typicallyCause::Unknown. If theos_err_reportingfeature is enabled, it will beCause::OSErr(oserr). In this case,oserrwill be the error fromlast_os_error().raw_os_error().Err(Error::ShrinkLargerNewLayout(old_layout.size(), new_layout.size()))ifold_layout.size() < new_layout.size().Err(Error::ZeroSizedLayout)ifnew_layout.size() == 0.