Rollup merge of #124980 - zachs18:rc-allocator, r=Amanieu

Generalize `fn allocator` for Rc/Arc.

Split out from #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
This commit is contained in:
Matthias Krüger 2024-07-12 14:37:56 +02:00 committed by GitHub
commit 65ea92d4a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 20 deletions

View File

@ -665,16 +665,6 @@ pub fn pin(value: T) -> Pin<Rc<T>> {
}
impl<T, A: Allocator> Rc<T, A> {
/// Returns a reference to the underlying allocator.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Rc::allocator(&r)` instead of `r.allocator()`. This
/// is so that there is no conflict with a method on the inner type.
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn allocator(this: &Self) -> &A {
&this.alloc
}
/// Constructs a new `Rc` in the provided allocator.
///
/// # Examples
@ -1331,6 +1321,17 @@ pub unsafe fn decrement_strong_count(ptr: *const T) {
}
impl<T: ?Sized, A: Allocator> Rc<T, A> {
/// Returns a reference to the underlying allocator.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Rc::allocator(&r)` instead of `r.allocator()`. This
/// is so that there is no conflict with a method on the inner type.
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn allocator(this: &Self) -> &A {
&this.alloc
}
/// Consumes the `Rc`, returning the wrapped pointer.
///
/// To avoid a memory leak the pointer must be converted back to an `Rc` using
@ -2994,6 +2995,13 @@ pub unsafe fn from_raw(ptr: *const T) -> Self {
}
impl<T: ?Sized, A: Allocator> Weak<T, A> {
/// Returns a reference to the underlying allocator.
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn allocator(&self) -> &A {
&self.alloc
}
/// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
///
/// The pointer is valid only if there are some strong references. The pointer may be dangling,

View File

@ -677,16 +677,6 @@ pub fn try_new_zeroed() -> Result<Arc<mem::MaybeUninit<T>>, AllocError> {
}
impl<T, A: Allocator> Arc<T, A> {
/// Returns a reference to the underlying allocator.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Arc::allocator(&a)` instead of `a.allocator()`. This
/// is so that there is no conflict with a method on the inner type.
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn allocator(this: &Self) -> &A {
&this.alloc
}
/// Constructs a new `Arc<T>` in the provided allocator.
///
/// # Examples
@ -1470,6 +1460,17 @@ pub unsafe fn decrement_strong_count(ptr: *const T) {
}
impl<T: ?Sized, A: Allocator> Arc<T, A> {
/// Returns a reference to the underlying allocator.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Arc::allocator(&a)` instead of `a.allocator()`. This
/// is so that there is no conflict with a method on the inner type.
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn allocator(this: &Self) -> &A {
&this.alloc
}
/// Consumes the `Arc`, returning the wrapped pointer.
///
/// To avoid a memory leak the pointer must be converted back to an `Arc` using
@ -2715,6 +2716,13 @@ pub unsafe fn from_raw(ptr: *const T) -> Self {
}
impl<T: ?Sized, A: Allocator> Weak<T, A> {
/// Returns a reference to the underlying allocator.
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn allocator(&self) -> &A {
&self.alloc
}
/// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
///
/// The pointer is valid only if there are some strong references. The pointer may be dangling,