From e60268076ff4c234cda9cff06f0d495fc6c3c8f6 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Wed, 22 Apr 2020 22:51:11 +0200 Subject: [PATCH] Add a "by reference" adaptor for `AllocRef` --- src/libcore/alloc/mod.rs | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/libcore/alloc/mod.rs b/src/libcore/alloc/mod.rs index e1892edb7c7..a3958ed6a30 100644 --- a/src/libcore/alloc/mod.rs +++ b/src/libcore/alloc/mod.rs @@ -364,4 +364,51 @@ unsafe fn shrink( } } } + + /// Creates a "by reference" adaptor for this instance of `AllocRef`. + /// + /// The returned adaptor also implements `AllocRef` and will simply borrow this. + #[inline(always)] + fn by_ref(&mut self) -> &mut Self { + self + } +} + +#[unstable(feature = "allocator_api", issue = "32838")] +unsafe impl AllocRef for &mut A +where + A: AllocRef + ?Sized, +{ + #[inline] + fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result { + (**self).alloc(layout, init) + } + + #[inline] + unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { + (**self).dealloc(ptr, layout) + } + + #[inline] + unsafe fn grow( + &mut self, + ptr: NonNull, + layout: Layout, + new_size: usize, + placement: ReallocPlacement, + init: AllocInit, + ) -> Result { + (**self).grow(ptr, layout, new_size, placement, init) + } + + #[inline] + unsafe fn shrink( + &mut self, + ptr: NonNull, + layout: Layout, + new_size: usize, + placement: ReallocPlacement, + ) -> Result { + (**self).shrink(ptr, layout, new_size, placement) + } } -- 2.44.0