- new_ptr.as_mut_ptr().copy_from_nonoverlapping(ptr.as_ptr(), new_layout.size());
- self.deallocate(ptr, old_layout);
+ // Safety: `new_ptr` and `ptr` are valid for reads/writes of
+ // `new_layout.size()` because of the invariants of shrink, which
+ // include `new_layout.size()` being smaller than (or equal to)
+ // `old_layout.size()`.
+ unsafe {
+ new_ptr.as_mut_ptr().copy_from_nonoverlapping(ptr.as_ptr(), new_layout.size());
+ }
+ // Safety: `ptr` is never used again is also an invariant which must
+ // be upheld by callers.
+ unsafe {
+ self.deallocate(ptr, old_layout);
+ }