Skip to content

Commit 3583cf0

Browse files
committed
allocator: refactor for stabilisation
f my gay ass puppy life
1 parent c315891 commit 3583cf0

12 files changed

Lines changed: 185 additions & 72 deletions

File tree

library/alloc/src/alloc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ unsafe extern "Rust" {
5757
#[lang = "global_alloc_ty"]
5858
pub struct Global;
5959

60+
#[unstable(feature = "allocator_api", issue = "32838")]
61+
unsafe impl core::alloc::AllocatorClone for Global {}
62+
63+
#[unstable(feature = "allocator_api", issue = "32838")]
64+
unsafe impl core::alloc::PinSafeAllocator for Global {}
65+
6066
/// Allocates memory with the global allocator.
6167
///
6268
/// This function forwards calls to the [`GlobalAlloc::alloc`] method

library/alloc/src/boxed.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ use core::task::{Context, Poll};
206206

207207
#[cfg(not(no_global_oom_handling))]
208208
use crate::alloc::handle_alloc_error;
209-
use crate::alloc::{AllocError, Allocator, Global, Layout};
209+
use crate::alloc::{
210+
AllocError, Allocator, AllocatorClone, AllocatorEq, Global, Layout, PinSafeAllocator,
211+
};
210212
use crate::raw_vec::RawVec;
211213
#[cfg(not(no_global_oom_handling))]
212-
use crate::str::from_boxed_utf8_unchecked;
214+
use crate::str::from_boxed_utf8_unchecked_in;
213215

214216
/// Conversion related impls for `Box<_>` (`From`, `downcast`, etc)
215217
mod convert;
@@ -710,7 +712,7 @@ impl<T, A: Allocator> Box<T, A> {
710712
#[inline(always)]
711713
pub fn pin_in(x: T, alloc: A) -> Pin<Self>
712714
where
713-
A: 'static + Allocator,
715+
A: PinSafeAllocator,
714716
{
715717
Self::into_pin(Self::new_in(x, alloc))
716718
}
@@ -1935,7 +1937,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
19351937
#[stable(feature = "box_into_pin", since = "1.63.0")]
19361938
pub fn into_pin(boxed: Self) -> Pin<Self>
19371939
where
1938-
A: 'static,
1940+
A: PinSafeAllocator,
19391941
{
19401942
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
19411943
// when `T: !Unpin`, so it's safe to pin it directly without any
@@ -2109,11 +2111,10 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
21092111

21102112
#[cfg(not(no_global_oom_handling))]
21112113
#[stable(feature = "box_slice_clone", since = "1.3.0")]
2112-
impl Clone for Box<str> {
2114+
impl<A: Allocator + Clone> Clone for Box<str, A> {
21132115
fn clone(&self) -> Self {
2114-
// this makes a copy of the data
2115-
let buf: Box<[u8]> = self.as_bytes().into();
2116-
unsafe { from_boxed_utf8_unchecked(buf) }
2116+
let buf = Box::clone_from_ref_in(self.as_bytes(), self.1.clone());
2117+
unsafe { from_boxed_utf8_unchecked_in(buf) }
21172118
}
21182119
}
21192120

@@ -2485,3 +2486,20 @@ unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {
24852486
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
24862487
}
24872488
}
2489+
2490+
#[unstable(feature = "allocator_api", issue = "32838")]
2491+
unsafe impl<T, A> AllocatorClone for Box<T, A>
2492+
where
2493+
T: AllocatorClone,
2494+
A: Allocator,
2495+
Box<T, A>: Clone,
2496+
{
2497+
}
2498+
2499+
#[unstable(feature = "allocator_api", issue = "32838")]
2500+
unsafe impl<T, A> AllocatorEq for Box<T, A>
2501+
where
2502+
T: AllocatorEq + ?Sized,
2503+
A: Allocator,
2504+
{
2505+
}

library/alloc/src/boxed/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::fmt;
55
use core::mem;
66
use core::pin::Pin;
77

8-
use crate::alloc::Allocator;
8+
use crate::alloc::{Allocator, PinSafeAllocator};
99
#[cfg(not(no_global_oom_handling))]
1010
use crate::borrow::Cow;
1111
use crate::boxed::Box;
@@ -38,7 +38,7 @@ impl<T> From<T> for Box<T> {
3838
#[stable(feature = "pin", since = "1.33.0")]
3939
impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Pin<Box<T, A>>
4040
where
41-
A: 'static,
41+
A: PinSafeAllocator,
4242
{
4343
/// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then
4444
/// `*boxed` will be pinned in memory and unable to be moved.

library/alloc/src/rc.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ use core::{borrow, fmt, hint};
268268

269269
#[cfg(not(no_global_oom_handling))]
270270
use crate::alloc::handle_alloc_error;
271-
use crate::alloc::{AllocError, Allocator, Global, Layout};
271+
use crate::alloc::{AllocError, Allocator, AllocatorClone, Global, Layout};
272272
use crate::borrow::{Cow, ToOwned};
273273
use crate::boxed::Box;
274274
#[cfg(not(no_global_oom_handling))]
@@ -1773,7 +1773,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
17731773
#[stable(feature = "rc_weak", since = "1.4.0")]
17741774
pub fn downgrade(this: &Self) -> Weak<T, A>
17751775
where
1776-
A: Clone,
1776+
A: AllocatorClone,
17771777
{
17781778
this.inner().inc_weak();
17791779
// Make sure we do not create a dangling Weak
@@ -1854,7 +1854,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
18541854
#[unstable(feature = "allocator_api", issue = "32838")]
18551855
pub unsafe fn increment_strong_count_in(ptr: *const T, alloc: A)
18561856
where
1857-
A: Clone,
1857+
A: AllocatorClone,
18581858
{
18591859
// Retain Rc, but don't touch refcount by wrapping in ManuallyDrop
18601860
let rc = unsafe { mem::ManuallyDrop::new(Rc::<T, A>::from_raw_in(ptr, alloc)) };
@@ -2030,7 +2030,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
20302030
}
20312031

20322032
#[cfg(not(no_global_oom_handling))]
2033-
impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Rc<T, A> {
2033+
impl<T: ?Sized + CloneToUninit, A: AllocatorClone> Rc<T, A> {
20342034
/// Makes a mutable reference into the given `Rc`.
20352035
///
20362036
/// If there are other `Rc` pointers to the same allocation, then `make_mut` will
@@ -2305,7 +2305,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
23052305

23062306
// Free the allocation without dropping its contents
23072307
let (bptr, alloc) = Box::into_raw_with_allocator(src);
2308-
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, alloc.by_ref());
2308+
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, &alloc);
23092309
drop(src);
23102310

23112311
Self::from_ptr_in(ptr, alloc)
@@ -2494,7 +2494,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Rc<T, A> {
24942494
}
24952495

24962496
#[stable(feature = "rust1", since = "1.0.0")]
2497-
impl<T: ?Sized, A: Allocator + Clone> Clone for Rc<T, A> {
2497+
impl<T: ?Sized, A: AllocatorClone> Clone for Rc<T, A> {
24982498
/// Makes a clone of the `Rc` pointer.
24992499
///
25002500
/// This creates another pointer to the same allocation, increasing the
@@ -2519,10 +2519,10 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Rc<T, A> {
25192519
}
25202520

25212521
#[unstable(feature = "ergonomic_clones", issue = "132290")]
2522-
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Rc<T, A> {}
2522+
impl<T: ?Sized, A: AllocatorClone> UseCloned for Rc<T, A> {}
25232523

25242524
#[unstable(feature = "share_trait", issue = "156756")]
2525-
impl<T: ?Sized, A: Allocator + Clone> Share for Rc<T, A> {}
2525+
impl<T: ?Sized, A: AllocatorClone> Share for Rc<T, A> {}
25262526

25272527
#[cfg(not(no_global_oom_handling))]
25282528
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3543,7 +3543,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
35433543
#[stable(feature = "rc_weak", since = "1.4.0")]
35443544
pub fn upgrade(&self) -> Option<Rc<T, A>>
35453545
where
3546-
A: Clone,
3546+
A: AllocatorClone,
35473547
{
35483548
let inner = self.inner()?;
35493549

@@ -3688,7 +3688,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Weak<T, A> {
36883688
}
36893689

36903690
#[stable(feature = "rc_weak", since = "1.4.0")]
3691-
impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
3691+
impl<T: ?Sized, A: AllocatorClone> Clone for Weak<T, A> {
36923692
/// Makes a clone of the `Weak` pointer that points to the same allocation.
36933693
///
36943694
/// # Examples
@@ -3710,7 +3710,7 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
37103710
}
37113711

37123712
#[unstable(feature = "ergonomic_clones", issue = "132290")]
3713-
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Weak<T, A> {}
3713+
impl<T: ?Sized, A: AllocatorClone> UseCloned for Weak<T, A> {}
37143714

37153715
#[stable(feature = "rc_weak", since = "1.4.0")]
37163716
impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
@@ -4410,7 +4410,7 @@ impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
44104410
}
44114411
}
44124412

4413-
impl<T: ?Sized, A: Allocator + Clone> UniqueRc<T, A> {
4413+
impl<T: ?Sized, A: AllocatorClone> UniqueRc<T, A> {
44144414
/// Creates a new weak reference to the `UniqueRc`.
44154415
///
44164416
/// Attempting to upgrade this weak reference will fail before the `UniqueRc` has been converted

library/alloc/src/str.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,15 @@ pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
794794
unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }
795795
}
796796

797+
#[doc(hidden)]
798+
#[unstable(feature = "allocator_api", issue = "32838")]
799+
pub unsafe fn from_boxed_utf8_unchecked_in<A: crate::alloc::Allocator>(
800+
v: Box<[u8], A>,
801+
) -> Box<str, A> {
802+
let (ptr, alloc) = Box::into_raw_with_allocator(v);
803+
unsafe { Box::from_raw_in(ptr as *mut str, alloc) }
804+
}
805+
797806
/// Converts leading ascii bytes in `s` by calling the `convert` function.
798807
///
799808
/// For better average performance, this happens in chunks of `2*size_of::<usize>()`.

library/alloc/src/sync.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use core::{borrow, fmt, hint};
3535

3636
#[cfg(not(no_global_oom_handling))]
3737
use crate::alloc::handle_alloc_error;
38-
use crate::alloc::{AllocError, Allocator, Global, Layout};
38+
use crate::alloc::{AllocError, Allocator, AllocatorClone, Global, Layout};
3939
use crate::borrow::{Cow, ToOwned};
4040
use crate::boxed::Box;
4141
use crate::rc::is_dangling;
@@ -1931,7 +1931,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
19311931
#[stable(feature = "arc_weak", since = "1.4.0")]
19321932
pub fn downgrade(this: &Self) -> Weak<T, A>
19331933
where
1934-
A: Clone,
1934+
A: AllocatorClone,
19351935
{
19361936
// This Relaxed is OK because we're checking the value in the CAS
19371937
// below.
@@ -2062,7 +2062,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
20622062
#[unstable(feature = "allocator_api", issue = "32838")]
20632063
pub unsafe fn increment_strong_count_in(ptr: *const T, alloc: A)
20642064
where
2065-
A: Clone,
2065+
A: AllocatorClone,
20662066
{
20672067
// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
20682068
let arc = unsafe { mem::ManuallyDrop::new(Arc::from_raw_in(ptr, alloc)) };
@@ -2250,7 +2250,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
22502250

22512251
// Free the allocation without dropping its contents
22522252
let (bptr, alloc) = Box::into_raw_with_allocator(src);
2253-
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, alloc.by_ref());
2253+
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, &alloc);
22542254
drop(src);
22552255

22562256
Self::from_ptr_in(ptr, alloc)
@@ -2376,7 +2376,7 @@ impl<T: TrivialClone> ArcFromSlice<T> for Arc<[T]> {
23762376
}
23772377

23782378
#[stable(feature = "rust1", since = "1.0.0")]
2379-
impl<T: ?Sized, A: Allocator + Clone> Clone for Arc<T, A> {
2379+
impl<T: ?Sized, A: AllocatorClone> Clone for Arc<T, A> {
23802380
/// Makes a clone of the `Arc` pointer.
23812381
///
23822382
/// This creates another pointer to the same allocation, increasing the
@@ -2430,10 +2430,10 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Arc<T, A> {
24302430
}
24312431

24322432
#[unstable(feature = "ergonomic_clones", issue = "132290")]
2433-
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Arc<T, A> {}
2433+
impl<T: ?Sized, A: AllocatorClone> UseCloned for Arc<T, A> {}
24342434

24352435
#[unstable(feature = "share_trait", issue = "156756")]
2436-
impl<T: ?Sized, A: Allocator + Clone> Share for Arc<T, A> {}
2436+
impl<T: ?Sized, A: AllocatorClone> Share for Arc<T, A> {}
24372437

24382438
#[stable(feature = "rust1", since = "1.0.0")]
24392439
impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
@@ -2455,7 +2455,7 @@ unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
24552455
impl<T: ?Sized> LegacyReceiver for Arc<T> {}
24562456

24572457
#[cfg(not(no_global_oom_handling))]
2458-
impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Arc<T, A> {
2458+
impl<T: ?Sized + CloneToUninit, A: AllocatorClone> Arc<T, A> {
24592459
/// Makes a mutable reference into the given `Arc`.
24602460
///
24612461
/// If there are other `Arc` pointers to the same allocation, then `make_mut` will
@@ -3274,7 +3274,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
32743274
#[stable(feature = "arc_weak", since = "1.4.0")]
32753275
pub fn upgrade(&self) -> Option<Arc<T, A>>
32763276
where
3277-
A: Clone,
3277+
A: AllocatorClone,
32783278
{
32793279
#[inline]
32803280
fn checked_increment(n: usize) -> Option<usize> {
@@ -3409,7 +3409,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
34093409
}
34103410

34113411
#[stable(feature = "arc_weak", since = "1.4.0")]
3412-
impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
3412+
impl<T: ?Sized, A: AllocatorClone> Clone for Weak<T, A> {
34133413
/// Makes a clone of the `Weak` pointer that points to the same allocation.
34143414
///
34153415
/// # Examples
@@ -3441,7 +3441,7 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
34413441
}
34423442

34433443
#[unstable(feature = "ergonomic_clones", issue = "132290")]
3444-
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Weak<T, A> {}
3444+
impl<T: ?Sized, A: AllocatorClone> UseCloned for Weak<T, A> {}
34453445

34463446
#[stable(feature = "downgraded_weak", since = "1.10.0")]
34473447
impl<T> Default for Weak<T> {
@@ -4029,7 +4029,7 @@ impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Arc<T, A> {
40294029

40304030
#[cfg(not(no_global_oom_handling))]
40314031
#[stable(feature = "shared_from_slice", since = "1.21.0")]
4032-
impl<T, A: Allocator + Clone> From<Vec<T, A>> for Arc<[T], A> {
4032+
impl<T, A: AllocatorClone> From<Vec<T, A>> for Arc<[T], A> {
40334033
/// Allocates a reference-counted slice and moves `v`'s items into it.
40344034
///
40354035
/// # Example
@@ -4839,7 +4839,7 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
48394839
}
48404840
}
48414841

4842-
impl<T: ?Sized, A: Allocator + Clone> UniqueArc<T, A> {
4842+
impl<T: ?Sized, A: AllocatorClone> UniqueArc<T, A> {
48434843
/// Creates a new weak reference to the `UniqueArc`.
48444844
///
48454845
/// Attempting to upgrade this weak reference will fail before the `UniqueArc` has been converted

0 commit comments

Comments
 (0)