Skip to content

Commit 2b228ce

Browse files
committed
Implement Allocator for &mut A where A: Allocator + ?Sized
1 parent dd7fda5 commit 2b228ce

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

library/core/src/alloc/mod.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,58 @@ where
421421
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
422422
}
423423
}
424+
425+
#[unstable(feature = "allocator_api", issue = "32838")]
426+
unsafe impl<A> Allocator for &mut A
427+
where
428+
A: Allocator + ?Sized,
429+
{
430+
#[inline]
431+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
432+
(**self).allocate(layout)
433+
}
434+
435+
#[inline]
436+
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
437+
(**self).allocate_zeroed(layout)
438+
}
439+
440+
#[inline]
441+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
442+
// SAFETY: the safety contract must be upheld by the caller
443+
unsafe { (**self).deallocate(ptr, layout) }
444+
}
445+
446+
#[inline]
447+
unsafe fn grow(
448+
&self,
449+
ptr: NonNull<u8>,
450+
old_layout: Layout,
451+
new_layout: Layout,
452+
) -> Result<NonNull<[u8]>, AllocError> {
453+
// SAFETY: the safety contract must be upheld by the caller
454+
unsafe { (**self).grow(ptr, old_layout, new_layout) }
455+
}
456+
457+
#[inline]
458+
unsafe fn grow_zeroed(
459+
&self,
460+
ptr: NonNull<u8>,
461+
old_layout: Layout,
462+
new_layout: Layout,
463+
) -> Result<NonNull<[u8]>, AllocError> {
464+
// SAFETY: the safety contract must be upheld by the caller
465+
unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
466+
}
467+
468+
#[inline]
469+
unsafe fn shrink(
470+
&self,
471+
ptr: NonNull<u8>,
472+
old_layout: Layout,
473+
new_layout: Layout,
474+
) -> Result<NonNull<[u8]>, AllocError> {
475+
// SAFETY: the safety contract must be upheld by the caller
476+
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
477+
}
478+
}

0 commit comments

Comments
 (0)