about summary refs log tree commit diff
path: root/src/libcore/alloc
diff options
context:
space:
mode:
authorTim Diekmann <tim.diekmann@3dvision.de>2020-04-22 22:51:11 +0200
committerTim Diekmann <tim.diekmann@3dvision.de>2020-04-22 22:51:11 +0200
commite60268076ff4c234cda9cff06f0d495fc6c3c8f6 (patch)
treea4ada4afc88e7fbf6265ffc328374c852d1a4db3 /src/libcore/alloc
parent82e90d64266b8a4b53935d629786e69610b33f25 (diff)
downloadrust-e60268076ff4c234cda9cff06f0d495fc6c3c8f6.tar.gz
rust-e60268076ff4c234cda9cff06f0d495fc6c3c8f6.zip
Add a "by reference" adaptor for `AllocRef`
Diffstat (limited to 'src/libcore/alloc')
-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 e1892edb7c7..a3958ed6a30 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)
+    }
 }