Skip to content

Commit 9ff05f3

Browse files
committed
Add alloc methods to Box and implement clone on slices
1 parent 0a2f062 commit 9ff05f3

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/liballoc/boxed.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,20 @@ impl<T: ?Sized, A: AllocRef> Box<T, A> {
729729
let reference = unsafe { &mut *manually_drop.0.as_ptr() };
730730
(reference, alloc)
731731
}
732+
733+
/// Returns a shared reference to the allocator backing this `Box`.
734+
#[inline]
735+
#[unstable(feature = "allocator_api", issue = "32838")]
736+
pub fn alloc(&self) -> &A {
737+
&self.1
738+
}
739+
740+
/// Returns a mutable reference to the allocator backing this `Box`.
741+
#[inline]
742+
#[unstable(feature = "allocator_api", issue = "32838")]
743+
pub fn alloc_mut(&mut self) -> &mut A {
744+
&mut self.1
745+
}
732746
}
733747

734748
impl<T: ?Sized, A: AllocRef> Box<T, A> {
@@ -794,7 +808,7 @@ impl<T: Clone, A: AllocRef + Clone> Clone for Box<T, A> {
794808
#[rustfmt::skip]
795809
#[inline]
796810
fn clone(&self) -> Self {
797-
Self::new_in((**self).clone(), self.1.clone())
811+
Self::new_in((**self).clone(), self.alloc().clone())
798812
}
799813

800814
/// Copies `source`'s contents into `self` without creating a new allocation.
@@ -825,7 +839,7 @@ impl<A: AllocRef + Clone> Clone for Box<str, A> {
825839
fn clone(&self) -> Self {
826840
let slice = self.as_bytes();
827841
let len = slice.len();
828-
let buf = RawVec::with_capacity_in(len, self.1.clone());
842+
let buf = RawVec::with_capacity_in(len, self.alloc().clone());
829843
unsafe {
830844
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
831845
from_boxed_utf8_unchecked(buf.into_box(slice.len()).assume_init())
@@ -1272,9 +1286,9 @@ impl<I> FromIterator<I> for Box<[I]> {
12721286
}
12731287

12741288
#[stable(feature = "box_slice_clone", since = "1.3.0")]
1275-
impl<T: Clone> Clone for Box<[T]> {
1289+
impl<T: Clone, A: AllocRef + Clone> Clone for Box<[T], A> {
12761290
fn clone(&self) -> Self {
1277-
self.to_vec().into_boxed_slice()
1291+
self.to_vec_in(self.alloc().clone()).into_boxed_slice()
12781292
}
12791293
}
12801294

0 commit comments

Comments
 (0)