about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-04-23 20:35:02 +0200
committerGitHub <noreply@github.com>2020-04-23 20:35:02 +0200
commit1363a4b3522feccdba4e4fc19cacb80f50e26e9a (patch)
treeb929e87fd9e67ee25eef5a93703c81d81ff3f4db
parent0959802a57987e4ba052f5b8d0fa110aa918ef1c (diff)
parente60268076ff4c234cda9cff06f0d495fc6c3c8f6 (diff)
downloadrust-1363a4b3522feccdba4e4fc19cacb80f50e26e9a.tar.gz
rust-1363a4b3522feccdba4e4fc19cacb80f50e26e9a.zip
Rollup merge of #71442 - TimDiekmann:allocref-mut-ref, r=Amanieu
Add a "by reference" adaptor for `AllocRef`

Fixes rust-lang/wg-allocators#53

r? @Amanieu
-rw-r--r--src/libcore/alloc/mod.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libcore/alloc/mod.rs b/src/libcore/alloc/mod.rs
index 86a6fa7f8ba..1346fbd4810 100644
--- a/src/libcore/alloc/mod.rs
+++ b/src/libcore/alloc/mod.rs
@@ -364,4 +364,51 @@ pub unsafe trait AllocRef {
             }
         }
     }
+
+    /// 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<A> AllocRef for &mut A
+where
+    A: AllocRef + ?Sized,
+{
+    #[inline]
+    fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
+        (**self).alloc(layout, init)
+    }
+
+    #[inline]
+    unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
+        (**self).dealloc(ptr, layout)
+    }
+
+    #[inline]
+    unsafe fn grow(
+        &mut self,
+        ptr: NonNull<u8>,
+        layout: Layout,
+        new_size: usize,
+        placement: ReallocPlacement,
+        init: AllocInit,
+    ) -> Result<MemoryBlock, AllocErr> {
+        (**self).grow(ptr, layout, new_size, placement, init)
+    }
+
+    #[inline]
+    unsafe fn shrink(
+        &mut self,
+        ptr: NonNull<u8>,
+        layout: Layout,
+        new_size: usize,
+        placement: ReallocPlacement,
+    ) -> Result<MemoryBlock, AllocErr> {
+        (**self).shrink(ptr, layout, new_size, placement)
+    }
 }