Skip to content

Commit 5943d07

Browse files
committed
Add Allocator proxy impls for Box, Rc, and Arc
This adds to the existing proxy impl for &T.
1 parent 1ef7943 commit 5943d07

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

library/alloc/src/boxed.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,3 +2241,55 @@ impl<E: Error> Error for Box<E> {
22412241
Error::provide(&**self, request);
22422242
}
22432243
}
2244+
2245+
#[unstable(feature = "allocator_api", issue = "32838")]
2246+
unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {
2247+
#[inline]
2248+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
2249+
(**self).allocate(layout)
2250+
}
2251+
2252+
#[inline]
2253+
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
2254+
(**self).allocate_zeroed(layout)
2255+
}
2256+
2257+
#[inline]
2258+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
2259+
// SAFETY: the safety contract must be upheld by the caller
2260+
unsafe { (**self).deallocate(ptr, layout) }
2261+
}
2262+
2263+
#[inline]
2264+
unsafe fn grow(
2265+
&self,
2266+
ptr: NonNull<u8>,
2267+
old_layout: Layout,
2268+
new_layout: Layout,
2269+
) -> Result<NonNull<[u8]>, AllocError> {
2270+
// SAFETY: the safety contract must be upheld by the caller
2271+
unsafe { (**self).grow(ptr, old_layout, new_layout) }
2272+
}
2273+
2274+
#[inline]
2275+
unsafe fn grow_zeroed(
2276+
&self,
2277+
ptr: NonNull<u8>,
2278+
old_layout: Layout,
2279+
new_layout: Layout,
2280+
) -> Result<NonNull<[u8]>, AllocError> {
2281+
// SAFETY: the safety contract must be upheld by the caller
2282+
unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
2283+
}
2284+
2285+
#[inline]
2286+
unsafe fn shrink(
2287+
&self,
2288+
ptr: NonNull<u8>,
2289+
old_layout: Layout,
2290+
new_layout: Layout,
2291+
) -> Result<NonNull<[u8]>, AllocError> {
2292+
// SAFETY: the safety contract must be upheld by the caller
2293+
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
2294+
}
2295+
}

library/alloc/src/rc.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4413,3 +4413,55 @@ impl<T: ?Sized, A: Allocator> Drop for UniqueRcUninit<T, A> {
44134413
}
44144414
}
44154415
}
4416+
4417+
#[unstable(feature = "allocator_api", issue = "32838")]
4418+
unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Rc<T, A> {
4419+
#[inline]
4420+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
4421+
(**self).allocate(layout)
4422+
}
4423+
4424+
#[inline]
4425+
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
4426+
(**self).allocate_zeroed(layout)
4427+
}
4428+
4429+
#[inline]
4430+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
4431+
// SAFETY: the safety contract must be upheld by the caller
4432+
unsafe { (**self).deallocate(ptr, layout) }
4433+
}
4434+
4435+
#[inline]
4436+
unsafe fn grow(
4437+
&self,
4438+
ptr: NonNull<u8>,
4439+
old_layout: Layout,
4440+
new_layout: Layout,
4441+
) -> Result<NonNull<[u8]>, AllocError> {
4442+
// SAFETY: the safety contract must be upheld by the caller
4443+
unsafe { (**self).grow(ptr, old_layout, new_layout) }
4444+
}
4445+
4446+
#[inline]
4447+
unsafe fn grow_zeroed(
4448+
&self,
4449+
ptr: NonNull<u8>,
4450+
old_layout: Layout,
4451+
new_layout: Layout,
4452+
) -> Result<NonNull<[u8]>, AllocError> {
4453+
// SAFETY: the safety contract must be upheld by the caller
4454+
unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
4455+
}
4456+
4457+
#[inline]
4458+
unsafe fn shrink(
4459+
&self,
4460+
ptr: NonNull<u8>,
4461+
old_layout: Layout,
4462+
new_layout: Layout,
4463+
) -> Result<NonNull<[u8]>, AllocError> {
4464+
// SAFETY: the safety contract must be upheld by the caller
4465+
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
4466+
}
4467+
}

library/alloc/src/sync.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4780,3 +4780,55 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for UniqueArc<T, A> {
47804780
unsafe { ptr::drop_in_place(&mut (*self.ptr.as_ptr()).data) };
47814781
}
47824782
}
4783+
4784+
#[unstable(feature = "allocator_api", issue = "32838")]
4785+
unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Arc<T, A> {
4786+
#[inline]
4787+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
4788+
(**self).allocate(layout)
4789+
}
4790+
4791+
#[inline]
4792+
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
4793+
(**self).allocate_zeroed(layout)
4794+
}
4795+
4796+
#[inline]
4797+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
4798+
// SAFETY: the safety contract must be upheld by the caller
4799+
unsafe { (**self).deallocate(ptr, layout) }
4800+
}
4801+
4802+
#[inline]
4803+
unsafe fn grow(
4804+
&self,
4805+
ptr: NonNull<u8>,
4806+
old_layout: Layout,
4807+
new_layout: Layout,
4808+
) -> Result<NonNull<[u8]>, AllocError> {
4809+
// SAFETY: the safety contract must be upheld by the caller
4810+
unsafe { (**self).grow(ptr, old_layout, new_layout) }
4811+
}
4812+
4813+
#[inline]
4814+
unsafe fn grow_zeroed(
4815+
&self,
4816+
ptr: NonNull<u8>,
4817+
old_layout: Layout,
4818+
new_layout: Layout,
4819+
) -> Result<NonNull<[u8]>, AllocError> {
4820+
// SAFETY: the safety contract must be upheld by the caller
4821+
unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
4822+
}
4823+
4824+
#[inline]
4825+
unsafe fn shrink(
4826+
&self,
4827+
ptr: NonNull<u8>,
4828+
old_layout: Layout,
4829+
new_layout: Layout,
4830+
) -> Result<NonNull<[u8]>, AllocError> {
4831+
// SAFETY: the safety contract must be upheld by the caller
4832+
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
4833+
}
4834+
}

0 commit comments

Comments
 (0)