]> git.lizzy.rs Git - rust.git/commitdiff
Add a "by reference" adaptor for `AllocRef`
authorTim Diekmann <tim.diekmann@3dvision.de>
Wed, 22 Apr 2020 20:51:11 +0000 (22:51 +0200)
committerTim Diekmann <tim.diekmann@3dvision.de>
Wed, 22 Apr 2020 20:51:11 +0000 (22:51 +0200)
src/libcore/alloc/mod.rs

index e1892edb7c7f3014852ecf9bbfcbd58d1fb30b2f..a3958ed6a30cc43dc317b80fca5cbecd6472b833 100644 (file)
@@ -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<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)
+    }
 }