mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
Add useful APIs to Unique(Arc|Rc)
This commit is contained in:
@@ -4326,6 +4326,13 @@ impl<T> UniqueRc<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Self::new_in(value, Global)
|
||||
}
|
||||
|
||||
/// Like [`new`](Self::new), but returns an error if the allocation
|
||||
/// fails, instead of calling [`handle_alloc_error`].
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
pub fn try_new(value: T) -> Result<Self, AllocError> {
|
||||
Self::try_new_in(value, Global)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A: Allocator> UniqueRc<T, A> {
|
||||
@@ -4337,8 +4344,8 @@ impl<T, A: Allocator> UniqueRc<T, A> {
|
||||
/// point to the new [`Rc`].
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
// #[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[must_use]
|
||||
pub fn new_in(value: T, alloc: A) -> Self {
|
||||
let (ptr, alloc) = Box::into_unique(Box::new_in(
|
||||
RcInner {
|
||||
@@ -4353,8 +4360,29 @@ impl<T, A: Allocator> UniqueRc<T, A> {
|
||||
Self { ptr: ptr.into(), _marker: PhantomData, _marker2: PhantomData, alloc }
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn unwrap_with_allocator(this: Self) -> (T, A) {
|
||||
/// Like [`new_in`](Self::new_in), but returns an error if the allocation
|
||||
/// fails, instead of calling [`handle_alloc_error`].
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
// #[unstable(feature = "allocator_api", issue = "32838")]
|
||||
pub fn try_new_in(value: T, alloc: A) -> Result<Self, AllocError> {
|
||||
let (ptr, alloc) = Box::into_non_null_with_allocator(Box::try_new_in(
|
||||
RcInner {
|
||||
strong: Cell::new(0),
|
||||
// keep one weak reference so if all the weak pointers that are created are dropped
|
||||
// the UniqueRc still stays valid.
|
||||
weak: Cell::new(1),
|
||||
value,
|
||||
},
|
||||
alloc,
|
||||
)?);
|
||||
Ok(Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc })
|
||||
}
|
||||
|
||||
/// Consumes the `UniqueRc`, returning its wrapped value and allocator.
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
// #[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[must_use]
|
||||
pub fn unwrap_with_allocator(this: Self) -> (T, A) {
|
||||
let inner_ptr = this.ptr;
|
||||
let (data_ptr, alloc) = Self::into_raw_with_allocator(this);
|
||||
|
||||
@@ -4368,6 +4396,13 @@ impl<T, A: Allocator> UniqueRc<T, A> {
|
||||
(val, alloc)
|
||||
}
|
||||
|
||||
/// Consumes the `UniqueRc`, returning its wrapped value.
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
pub fn unwrap(this: Self) -> T {
|
||||
Self::unwrap_with_allocator(this).0
|
||||
}
|
||||
|
||||
/// Maps the value in a `UniqueRc`, reusing the allocation if possible.
|
||||
///
|
||||
/// `f` is called on a reference to the value in the `UniqueRc`, and the result is returned,
|
||||
@@ -4399,11 +4434,10 @@ impl<T, A: Allocator> UniqueRc<T, A> {
|
||||
unsafe {
|
||||
let (ptr, alloc) = UniqueRc::into_raw_with_allocator(this);
|
||||
let value = ptr.read();
|
||||
let mut allocation =
|
||||
let allocation =
|
||||
UniqueRc::from_raw_with_allocator(ptr.cast::<mem::MaybeUninit<U>>(), alloc);
|
||||
|
||||
allocation.write(f(value));
|
||||
allocation.assume_init()
|
||||
UniqueRc::write(allocation, f(value))
|
||||
}
|
||||
} else {
|
||||
let (val, alloc) = UniqueRc::unwrap_with_allocator(this);
|
||||
@@ -4450,13 +4484,12 @@ impl<T, A: Allocator> UniqueRc<T, A> {
|
||||
unsafe {
|
||||
let (ptr, alloc) = UniqueRc::into_raw_with_allocator(this);
|
||||
let value = ptr.read();
|
||||
let mut allocation = UniqueRc::from_raw_with_allocator(
|
||||
let allocation = UniqueRc::from_raw_with_allocator(
|
||||
ptr.cast::<mem::MaybeUninit<R::Output>>(),
|
||||
alloc,
|
||||
);
|
||||
|
||||
allocation.write(f(value)?);
|
||||
try { allocation.assume_init() }
|
||||
try { UniqueRc::write(allocation, f(value)?) }
|
||||
}
|
||||
} else {
|
||||
let (val, alloc) = UniqueRc::unwrap_with_allocator(this);
|
||||
@@ -4484,7 +4517,6 @@ impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn into_raw_with_allocator(this: Self) -> (*const T, A) {
|
||||
let this = ManuallyDrop::new(this);
|
||||
// SAFETY: The copy of the allocator stored in `this` is forgotten
|
||||
@@ -4526,7 +4558,6 @@ impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
|
||||
unsafe { self.ptr.as_ref() }
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn as_ptr(this: &Self) -> *const T {
|
||||
let ptr: *mut RcInner<T> = NonNull::as_ptr(this.ptr);
|
||||
|
||||
@@ -4537,7 +4568,6 @@ impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn into_inner_with_allocator(this: Self) -> (NonNull<RcInner<T>>, A) {
|
||||
let this = mem::ManuallyDrop::new(this);
|
||||
// SAFETY: Pointer is valid for reads.
|
||||
@@ -4545,7 +4575,6 @@ impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
unsafe fn from_inner_in(ptr: NonNull<RcInner<T>>, alloc: A) -> Self {
|
||||
Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc }
|
||||
}
|
||||
@@ -4567,9 +4596,35 @@ impl<T: ?Sized, A: AllocatorClone> UniqueRc<T, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
impl<T, A: Allocator> UniqueRc<mem::MaybeUninit<T>, A> {
|
||||
unsafe fn assume_init(self) -> UniqueRc<T, A> {
|
||||
/// Writes the value and converts to `UniqueRc<T, A>`.
|
||||
///
|
||||
/// This method converts similarly to [`assume_init`](Self::assume_init) but
|
||||
/// writes `value` into it before conversion, thus guaranteeing safety.
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
pub fn write(mut this: Self, value: T) -> UniqueRc<T, A> {
|
||||
// SAFETY: Writing initialises the wrapped value.
|
||||
unsafe {
|
||||
this.write(value);
|
||||
this.assume_init()
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts to `UniqueRc<T, A>`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// As with [`MaybeUninit::assume_init`],
|
||||
/// it is up to the caller to guarantee that the value
|
||||
/// really is in an initialized state.
|
||||
/// Calling this when the content is not yet fully initialized
|
||||
/// causes immediate undefined behavior.
|
||||
///
|
||||
/// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
pub unsafe fn assume_init(self) -> UniqueRc<T, A> {
|
||||
let (ptr, alloc) = UniqueRc::into_inner_with_allocator(self);
|
||||
// SAFETY: Upheld by caller.
|
||||
unsafe { UniqueRc::from_inner_in(ptr.cast(), alloc) }
|
||||
|
||||
@@ -4803,6 +4803,13 @@ impl<T> UniqueArc<T, Global> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Self::new_in(value, Global)
|
||||
}
|
||||
|
||||
/// Like [`new`](Self::new), but returns an error if the allocation
|
||||
/// fails, instead of calling [`handle_alloc_error`].
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
pub fn try_new(value: T) -> Result<Self, AllocError> {
|
||||
Self::try_new_in(value, Global)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A: Allocator> UniqueArc<T, A> {
|
||||
@@ -4814,8 +4821,8 @@ impl<T, A: Allocator> UniqueArc<T, A> {
|
||||
/// point to the new [`Arc`].
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
// #[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[must_use]
|
||||
pub fn new_in(data: T, alloc: A) -> Self {
|
||||
let (ptr, alloc) = Box::into_unique(Box::new_in(
|
||||
ArcInner {
|
||||
@@ -4830,8 +4837,29 @@ impl<T, A: Allocator> UniqueArc<T, A> {
|
||||
Self { ptr: ptr.into(), _marker: PhantomData, _marker2: PhantomData, alloc }
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn unwrap_with_allocator(this: Self) -> (T, A) {
|
||||
/// Like [`new_in`](Self::new_in), but returns an error if the allocation
|
||||
/// fails, instead of calling [`handle_alloc_error`].
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
// #[unstable(feature = "allocator_api", issue = "32838")]
|
||||
pub fn try_new_in(data: T, alloc: A) -> Result<Self, AllocError> {
|
||||
let (ptr, alloc) = Box::into_non_null_with_allocator(Box::try_new_in(
|
||||
ArcInner {
|
||||
strong: atomic::AtomicUsize::new(0),
|
||||
// keep one weak reference so if all the weak pointers that are created are dropped
|
||||
// the UniqueArc still stays valid.
|
||||
weak: atomic::AtomicUsize::new(1),
|
||||
data,
|
||||
},
|
||||
alloc,
|
||||
)?);
|
||||
Ok(Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc })
|
||||
}
|
||||
|
||||
/// Consumes the `UniqueArc`, returning its wrapped value and allocator.
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
// #[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[must_use]
|
||||
pub fn unwrap_with_allocator(this: Self) -> (T, A) {
|
||||
let inner_ptr = this.ptr;
|
||||
let (data_ptr, alloc) = Self::into_raw_with_allocator(this);
|
||||
|
||||
@@ -4839,11 +4867,19 @@ impl<T, A: Allocator> UniqueArc<T, A> {
|
||||
// We do not use the data inside ever again.
|
||||
let val = unsafe { data_ptr.read() };
|
||||
|
||||
// Drop the strong-weak ref
|
||||
drop(Weak { ptr: inner_ptr, alloc: &alloc });
|
||||
|
||||
(val, alloc)
|
||||
}
|
||||
|
||||
/// Consumes the `UniqueArc`, returning its wrapped value.
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
pub fn unwrap(this: Self) -> T {
|
||||
Self::unwrap_with_allocator(this).0
|
||||
}
|
||||
|
||||
/// Maps the value in a `UniqueArc`, reusing the allocation if possible.
|
||||
///
|
||||
/// `f` is called on a reference to the value in the `UniqueArc`, and the result is returned,
|
||||
@@ -4875,11 +4911,10 @@ impl<T, A: Allocator> UniqueArc<T, A> {
|
||||
unsafe {
|
||||
let (ptr, alloc) = UniqueArc::into_raw_with_allocator(this);
|
||||
let value = ptr.read();
|
||||
let mut allocation =
|
||||
let allocation =
|
||||
UniqueArc::from_raw_with_allocator(ptr.cast::<mem::MaybeUninit<U>>(), alloc);
|
||||
|
||||
allocation.write(f(value));
|
||||
allocation.assume_init()
|
||||
UniqueArc::write(allocation, f(value))
|
||||
}
|
||||
} else {
|
||||
let (val, alloc) = UniqueArc::unwrap_with_allocator(this);
|
||||
@@ -4926,13 +4961,12 @@ impl<T, A: Allocator> UniqueArc<T, A> {
|
||||
unsafe {
|
||||
let (ptr, alloc) = UniqueArc::into_raw_with_allocator(this);
|
||||
let value = ptr.read();
|
||||
let mut allocation = UniqueArc::from_raw_with_allocator(
|
||||
let allocation = UniqueArc::from_raw_with_allocator(
|
||||
ptr.cast::<mem::MaybeUninit<R::Output>>(),
|
||||
alloc,
|
||||
);
|
||||
|
||||
allocation.write(f(value)?);
|
||||
try { allocation.assume_init() }
|
||||
try { UniqueArc::write(allocation, f(value)?) }
|
||||
}
|
||||
} else {
|
||||
let (val, alloc) = UniqueArc::unwrap_with_allocator(this);
|
||||
@@ -4960,7 +4994,6 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn into_raw_with_allocator(this: Self) -> (*const T, A) {
|
||||
let this = ManuallyDrop::new(this);
|
||||
// SAFETY: The copy of the allocator stored in `this` is forgotten
|
||||
@@ -5003,7 +5036,6 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
|
||||
unsafe { self.ptr.as_ref() }
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn as_ptr(this: &Self) -> *const T {
|
||||
let ptr: *mut ArcInner<T> = NonNull::as_ptr(this.ptr);
|
||||
|
||||
@@ -5014,7 +5046,6 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
fn into_inner_with_allocator(this: Self) -> (NonNull<ArcInner<T>>, A) {
|
||||
let this = mem::ManuallyDrop::new(this);
|
||||
// SAFETY: Pointer is valid for reads and only read once.
|
||||
@@ -5022,7 +5053,6 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
unsafe fn from_inner_in(ptr: NonNull<ArcInner<T>>, alloc: A) -> Self {
|
||||
Self { ptr, _marker: PhantomData, _marker2: PhantomData, alloc }
|
||||
}
|
||||
@@ -5056,9 +5086,35 @@ impl<T: ?Sized, A: AllocatorClone> UniqueArc<T, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
impl<T, A: Allocator> UniqueArc<mem::MaybeUninit<T>, A> {
|
||||
unsafe fn assume_init(self) -> UniqueArc<T, A> {
|
||||
/// Writes the value and converts to `UniqueArc<T, A>`.
|
||||
///
|
||||
/// This method converts similarly to [`assume_init`](Self::assume_init) but
|
||||
/// writes `value` into it before conversion, thus guaranteeing safety.
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
pub fn write(mut this: Self, value: T) -> UniqueArc<T, A> {
|
||||
// SAFETY: Writing initialises the wrapped value.
|
||||
unsafe {
|
||||
this.write(value);
|
||||
this.assume_init()
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts to `UniqueArc<T, A>`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// As with [`MaybeUninit::assume_init`],
|
||||
/// it is up to the caller to guarantee that the value
|
||||
/// really is in an initialized state.
|
||||
/// Calling this when the content is not yet fully initialized
|
||||
/// causes immediate undefined behavior.
|
||||
///
|
||||
/// [`MaybeUninit::assume_init`]: mem::MaybeUninit::assume_init
|
||||
#[unstable(feature = "unique_rc_arc", issue = "112566")]
|
||||
#[must_use]
|
||||
pub unsafe fn assume_init(self) -> UniqueArc<T, A> {
|
||||
let (ptr, alloc) = UniqueArc::into_inner_with_allocator(self);
|
||||
// SAFETY: Upheld by caller.
|
||||
unsafe { UniqueArc::from_inner_in(ptr.cast(), alloc) }
|
||||
|
||||
Reference in New Issue
Block a user