Skip to content

Commit e602680

Browse files
committed
Add a "by reference" adaptor for AllocRef
1 parent 82e90d6 commit e602680

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/libcore/alloc/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,51 @@ pub unsafe trait AllocRef {
364364
}
365365
}
366366
}
367+
368+
/// Creates a "by reference" adaptor for this instance of `AllocRef`.
369+
///
370+
/// The returned adaptor also implements `AllocRef` and will simply borrow this.
371+
#[inline(always)]
372+
fn by_ref(&mut self) -> &mut Self {
373+
self
374+
}
375+
}
376+
377+
#[unstable(feature = "allocator_api", issue = "32838")]
378+
unsafe impl<A> AllocRef for &mut A
379+
where
380+
A: AllocRef + ?Sized,
381+
{
382+
#[inline]
383+
fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
384+
(**self).alloc(layout, init)
385+
}
386+
387+
#[inline]
388+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
389+
(**self).dealloc(ptr, layout)
390+
}
391+
392+
#[inline]
393+
unsafe fn grow(
394+
&mut self,
395+
ptr: NonNull<u8>,
396+
layout: Layout,
397+
new_size: usize,
398+
placement: ReallocPlacement,
399+
init: AllocInit,
400+
) -> Result<MemoryBlock, AllocErr> {
401+
(**self).grow(ptr, layout, new_size, placement, init)
402+
}
403+
404+
#[inline]
405+
unsafe fn shrink(
406+
&mut self,
407+
ptr: NonNull<u8>,
408+
layout: Layout,
409+
new_size: usize,
410+
placement: ReallocPlacement,
411+
) -> Result<MemoryBlock, AllocErr> {
412+
(**self).shrink(ptr, layout, new_size, placement)
413+
}
367414
}

0 commit comments

Comments
 (0)