Skip to content

Commit 1e5cd21

Browse files
authored
Rollup merge of rust-lang#124980 - zachs18:rc-allocator, r=Amanieu
Generalize `fn allocator` for Rc/Arc. Split out from rust-lang#119761 - For `Rc`/`Arc`, the existing associated `fn`s are changed to allow unsized pointees. - For `Weak`s, new methods are added. `````@rustbot````` label +A-allocators
2 parents 20bfac6 + 7db52fc commit 1e5cd21

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

alloc/src/rc.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -665,16 +665,6 @@ impl<T> Rc<T> {
665665
}
666666

667667
impl<T, A: Allocator> Rc<T, A> {
668-
/// Returns a reference to the underlying allocator.
669-
///
670-
/// Note: this is an associated function, which means that you have
671-
/// to call it as `Rc::allocator(&r)` instead of `r.allocator()`. This
672-
/// is so that there is no conflict with a method on the inner type.
673-
#[inline]
674-
#[unstable(feature = "allocator_api", issue = "32838")]
675-
pub fn allocator(this: &Self) -> &A {
676-
&this.alloc
677-
}
678668
/// Constructs a new `Rc` in the provided allocator.
679669
///
680670
/// # Examples
@@ -1331,6 +1321,17 @@ impl<T: ?Sized> Rc<T> {
13311321
}
13321322

13331323
impl<T: ?Sized, A: Allocator> Rc<T, A> {
1324+
/// Returns a reference to the underlying allocator.
1325+
///
1326+
/// Note: this is an associated function, which means that you have
1327+
/// to call it as `Rc::allocator(&r)` instead of `r.allocator()`. This
1328+
/// is so that there is no conflict with a method on the inner type.
1329+
#[inline]
1330+
#[unstable(feature = "allocator_api", issue = "32838")]
1331+
pub fn allocator(this: &Self) -> &A {
1332+
&this.alloc
1333+
}
1334+
13341335
/// Consumes the `Rc`, returning the wrapped pointer.
13351336
///
13361337
/// To avoid a memory leak the pointer must be converted back to an `Rc` using
@@ -2994,6 +2995,13 @@ impl<T: ?Sized> Weak<T> {
29942995
}
29952996

29962997
impl<T: ?Sized, A: Allocator> Weak<T, A> {
2998+
/// Returns a reference to the underlying allocator.
2999+
#[inline]
3000+
#[unstable(feature = "allocator_api", issue = "32838")]
3001+
pub fn allocator(&self) -> &A {
3002+
&self.alloc
3003+
}
3004+
29973005
/// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
29983006
///
29993007
/// The pointer is valid only if there are some strong references. The pointer may be dangling,

alloc/src/sync.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -677,16 +677,6 @@ impl<T> Arc<T> {
677677
}
678678

679679
impl<T, A: Allocator> Arc<T, A> {
680-
/// Returns a reference to the underlying allocator.
681-
///
682-
/// Note: this is an associated function, which means that you have
683-
/// to call it as `Arc::allocator(&a)` instead of `a.allocator()`. This
684-
/// is so that there is no conflict with a method on the inner type.
685-
#[inline]
686-
#[unstable(feature = "allocator_api", issue = "32838")]
687-
pub fn allocator(this: &Self) -> &A {
688-
&this.alloc
689-
}
690680
/// Constructs a new `Arc<T>` in the provided allocator.
691681
///
692682
/// # Examples
@@ -1470,6 +1460,17 @@ impl<T: ?Sized> Arc<T> {
14701460
}
14711461

14721462
impl<T: ?Sized, A: Allocator> Arc<T, A> {
1463+
/// Returns a reference to the underlying allocator.
1464+
///
1465+
/// Note: this is an associated function, which means that you have
1466+
/// to call it as `Arc::allocator(&a)` instead of `a.allocator()`. This
1467+
/// is so that there is no conflict with a method on the inner type.
1468+
#[inline]
1469+
#[unstable(feature = "allocator_api", issue = "32838")]
1470+
pub fn allocator(this: &Self) -> &A {
1471+
&this.alloc
1472+
}
1473+
14731474
/// Consumes the `Arc`, returning the wrapped pointer.
14741475
///
14751476
/// To avoid a memory leak the pointer must be converted back to an `Arc` using
@@ -2715,6 +2716,13 @@ impl<T: ?Sized> Weak<T> {
27152716
}
27162717

27172718
impl<T: ?Sized, A: Allocator> Weak<T, A> {
2719+
/// Returns a reference to the underlying allocator.
2720+
#[inline]
2721+
#[unstable(feature = "allocator_api", issue = "32838")]
2722+
pub fn allocator(&self) -> &A {
2723+
&self.alloc
2724+
}
2725+
27182726
/// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
27192727
///
27202728
/// The pointer is valid only if there are some strong references. The pointer may be dangling,

0 commit comments

Comments
 (0)