Skip to content
/ rust Public
forked from rust-lang/rust

Commit b542a2b

Browse files
committed
Add more APIs for UniqueArc
1 parent 9e5cca5 commit b542a2b

File tree

1 file changed

+250
-5
lines changed

1 file changed

+250
-5
lines changed

library/alloc/src/sync.rs

+250-5
Original file line numberDiff line numberDiff line change
@@ -4073,7 +4073,7 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
40734073
/// reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
40744074
/// references will fail unless the `UniqueArc` they point to has been converted into a regular `Arc`.
40754075
///
4076-
/// Because they are uniquely owned, the contents of a `UniqueArc` can be freely mutated. A common
4076+
/// Because it is uniquely owned, the contents of a `UniqueArc` can be freely mutated. A common
40774077
/// use case is to have an object be mutable during its initialization phase but then have it become
40784078
/// immutable and converted to a normal `Arc`.
40794079
///
@@ -4084,7 +4084,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
40844084
/// use std::sync::{Arc, Weak, UniqueArc};
40854085
///
40864086
/// struct Gadget {
4087-
/// #[allow(dead_code)]
40884087
/// me: Weak<Gadget>,
40894088
/// }
40904089
///
@@ -4104,7 +4103,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
41044103
/// previous example, `UniqueArc` allows for more flexibility in the construction of cyclic data,
41054104
/// including fallible or async constructors.
41064105
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4107-
#[derive(Debug)]
41084106
pub struct UniqueArc<
41094107
T: ?Sized,
41104108
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
@@ -4119,13 +4117,248 @@ pub struct UniqueArc<
41194117
}
41204118

41214119
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4120+
unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Send> Send for UniqueArc<T, A> {}
4121+
4122+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4123+
unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Sync> Sync for UniqueArc<T, A> {}
4124+
4125+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4126+
// #[unstable(feature = "coerce_unsized", issue = "18598")]
41224127
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<UniqueArc<U, A>>
41234128
for UniqueArc<T, A>
41244129
{
41254130
}
41264131

4127-
// Depends on A = Global
4128-
impl<T> UniqueArc<T> {
4132+
//#[unstable(feature = "unique_rc_arc", issue = "112566")]
4133+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
4134+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<UniqueArc<U>> for UniqueArc<T> {}
4135+
4136+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4137+
impl<T: ?Sized + fmt::Display, A: Allocator> fmt::Display for UniqueArc<T, A> {
4138+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4139+
fmt::Display::fmt(&**self, f)
4140+
}
4141+
}
4142+
4143+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4144+
impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for UniqueArc<T, A> {
4145+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4146+
fmt::Debug::fmt(&**self, f)
4147+
}
4148+
}
4149+
4150+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4151+
impl<T: ?Sized, A: Allocator> fmt::Pointer for UniqueArc<T, A> {
4152+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4153+
fmt::Pointer::fmt(&(&raw const **self), f)
4154+
}
4155+
}
4156+
4157+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4158+
impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for UniqueArc<T, A> {
4159+
fn borrow(&self) -> &T {
4160+
&**self
4161+
}
4162+
}
4163+
4164+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4165+
impl<T: ?Sized, A: Allocator> borrow::BorrowMut<T> for UniqueArc<T, A> {
4166+
fn borrow_mut(&mut self) -> &mut T {
4167+
&mut **self
4168+
}
4169+
}
4170+
4171+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4172+
impl<T: ?Sized, A: Allocator> AsRef<T> for UniqueArc<T, A> {
4173+
fn as_ref(&self) -> &T {
4174+
&**self
4175+
}
4176+
}
4177+
4178+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4179+
impl<T: ?Sized, A: Allocator> AsMut<T> for UniqueArc<T, A> {
4180+
fn as_mut(&mut self) -> &mut T {
4181+
&mut **self
4182+
}
4183+
}
4184+
4185+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4186+
impl<T: ?Sized, A: Allocator> Unpin for UniqueArc<T, A> {}
4187+
4188+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4189+
impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for UniqueArc<T, A> {
4190+
/// Equality for two `UniqueArc`s.
4191+
///
4192+
/// Two `UniqueArc`s are equal if their inner values are equal.
4193+
///
4194+
/// # Examples
4195+
///
4196+
/// ```
4197+
/// #![feature(unique_rc_arc)]
4198+
/// use std::sync::UniqueArc;
4199+
///
4200+
/// let five = UniqueArc::new(5);
4201+
///
4202+
/// assert!(five == UniqueArc::new(5));
4203+
/// ```
4204+
#[inline]
4205+
fn eq(&self, other: &Self) -> bool {
4206+
PartialEq::eq(&**self, &**other)
4207+
}
4208+
4209+
/// Inequality for two `UniqueArc`s.
4210+
///
4211+
/// Two `UniqueArc`s are not equal if their inner values are not equal.
4212+
///
4213+
/// # Examples
4214+
///
4215+
/// ```
4216+
/// #![feature(unique_rc_arc)]
4217+
/// use std::sync::UniqueArc;
4218+
///
4219+
/// let five = UniqueArc::new(5);
4220+
///
4221+
/// assert!(five != UniqueArc::new(6));
4222+
/// ```
4223+
#[inline]
4224+
fn ne(&self, other: &Self) -> bool {
4225+
PartialEq::ne(&**self, &**other)
4226+
}
4227+
}
4228+
4229+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4230+
impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for UniqueArc<T, A> {
4231+
/// Partial comparison for two `UniqueArc`s.
4232+
///
4233+
/// The two are compared by calling `partial_cmp()` on their inner values.
4234+
///
4235+
/// # Examples
4236+
///
4237+
/// ```
4238+
/// #![feature(unique_rc_arc)]
4239+
/// use std::sync::UniqueArc;
4240+
/// use std::cmp::Ordering;
4241+
///
4242+
/// let five = UniqueArc::new(5);
4243+
///
4244+
/// assert_eq!(Some(Ordering::Less), five.partial_cmp(&UniqueArc::new(6)));
4245+
/// ```
4246+
#[inline(always)]
4247+
fn partial_cmp(&self, other: &UniqueArc<T, A>) -> Option<Ordering> {
4248+
(**self).partial_cmp(&**other)
4249+
}
4250+
4251+
/// Less-than comparison for two `UniqueArc`s.
4252+
///
4253+
/// The two are compared by calling `<` on their inner values.
4254+
///
4255+
/// # Examples
4256+
///
4257+
/// ```
4258+
/// #![feature(unique_rc_arc)]
4259+
/// use std::sync::UniqueArc;
4260+
///
4261+
/// let five = UniqueArc::new(5);
4262+
///
4263+
/// assert!(five < UniqueArc::new(6));
4264+
/// ```
4265+
#[inline(always)]
4266+
fn lt(&self, other: &UniqueArc<T, A>) -> bool {
4267+
**self < **other
4268+
}
4269+
4270+
/// 'Less than or equal to' comparison for two `UniqueArc`s.
4271+
///
4272+
/// The two are compared by calling `<=` on their inner values.
4273+
///
4274+
/// # Examples
4275+
///
4276+
/// ```
4277+
/// #![feature(unique_rc_arc)]
4278+
/// use std::sync::UniqueArc;
4279+
///
4280+
/// let five = UniqueArc::new(5);
4281+
///
4282+
/// assert!(five <= UniqueArc::new(5));
4283+
/// ```
4284+
#[inline(always)]
4285+
fn le(&self, other: &UniqueArc<T, A>) -> bool {
4286+
**self <= **other
4287+
}
4288+
4289+
/// Greater-than comparison for two `UniqueArc`s.
4290+
///
4291+
/// The two are compared by calling `>` on their inner values.
4292+
///
4293+
/// # Examples
4294+
///
4295+
/// ```
4296+
/// #![feature(unique_rc_arc)]
4297+
/// use std::sync::UniqueArc;
4298+
///
4299+
/// let five = UniqueArc::new(5);
4300+
///
4301+
/// assert!(five > UniqueArc::new(4));
4302+
/// ```
4303+
#[inline(always)]
4304+
fn gt(&self, other: &UniqueArc<T, A>) -> bool {
4305+
**self > **other
4306+
}
4307+
4308+
/// 'Greater than or equal to' comparison for two `UniqueArc`s.
4309+
///
4310+
/// The two are compared by calling `>=` on their inner values.
4311+
///
4312+
/// # Examples
4313+
///
4314+
/// ```
4315+
/// #![feature(unique_rc_arc)]
4316+
/// use std::sync::UniqueArc;
4317+
///
4318+
/// let five = UniqueArc::new(5);
4319+
///
4320+
/// assert!(five >= UniqueArc::new(5));
4321+
/// ```
4322+
#[inline(always)]
4323+
fn ge(&self, other: &UniqueArc<T, A>) -> bool {
4324+
**self >= **other
4325+
}
4326+
}
4327+
4328+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4329+
impl<T: ?Sized + Ord, A: Allocator> Ord for UniqueArc<T, A> {
4330+
/// Comparison for two `UniqueArc`s.
4331+
///
4332+
/// The two are compared by calling `cmp()` on their inner values.
4333+
///
4334+
/// # Examples
4335+
///
4336+
/// ```
4337+
/// #![feature(unique_rc_arc)]
4338+
/// use std::sync::UniqueArc;
4339+
/// use std::cmp::Ordering;
4340+
///
4341+
/// let five = UniqueArc::new(5);
4342+
///
4343+
/// assert_eq!(Ordering::Less, five.cmp(&UniqueArc::new(6)));
4344+
/// ```
4345+
#[inline]
4346+
fn cmp(&self, other: &UniqueArc<T, A>) -> Ordering {
4347+
(**self).cmp(&**other)
4348+
}
4349+
}
4350+
4351+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4352+
impl<T: ?Sized + Eq, A: Allocator> Eq for UniqueArc<T, A> {}
4353+
4354+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4355+
impl<T: ?Sized + Hash, A: Allocator> Hash for UniqueArc<T, A> {
4356+
fn hash<H: Hasher>(&self, state: &mut H) {
4357+
(**self).hash(state);
4358+
}
4359+
}
4360+
4361+
impl<T> UniqueArc<T, Global> {
41294362
/// Creates a new `UniqueArc`.
41304363
///
41314364
/// Weak references to this `UniqueArc` can be created with [`UniqueArc::downgrade`]. Upgrading
@@ -4134,6 +4367,7 @@ impl<T> UniqueArc<T> {
41344367
/// point to the new [`Arc`].
41354368
#[cfg(not(no_global_oom_handling))]
41364369
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4370+
#[must_use]
41374371
pub fn new(value: T) -> Self {
41384372
Self::new_in(value, Global)
41394373
}
@@ -4148,6 +4382,8 @@ impl<T, A: Allocator> UniqueArc<T, A> {
41484382
/// point to the new [`Arc`].
41494383
#[cfg(not(no_global_oom_handling))]
41504384
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4385+
#[must_use]
4386+
// #[unstable(feature = "allocator_api", issue = "32838")]
41514387
pub fn new_in(data: T, alloc: A) -> Self {
41524388
let (ptr, alloc) = Box::into_unique(Box::new_in(
41534389
ArcInner {
@@ -4172,6 +4408,7 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
41724408
/// Any weak references created before this method is called can now be upgraded to strong
41734409
/// references.
41744410
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4411+
#[must_use]
41754412
pub fn into_arc(this: Self) -> Arc<T, A> {
41764413
let this = ManuallyDrop::new(this);
41774414

@@ -4195,6 +4432,7 @@ impl<T: ?Sized, A: Allocator + Clone> UniqueArc<T, A> {
41954432
/// Attempting to upgrade this weak reference will fail before the `UniqueArc` has been converted
41964433
/// to a [`Arc`] using [`UniqueArc::into_arc`].
41974434
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4435+
#[must_use]
41984436
pub fn downgrade(this: &Self) -> Weak<T, A> {
41994437
// Using a relaxed ordering is alright here, as knowledge of the
42004438
// original reference prevents other threads from erroneously deleting
@@ -4236,10 +4474,17 @@ impl<T: ?Sized, A: Allocator> DerefMut for UniqueArc<T, A> {
42364474
// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we
42374475
// have unique ownership and therefore it's safe to make a mutable reference because
42384476
// `UniqueArc` owns the only strong reference to itself.
4477+
// We also need to be careful to only create a mutable reference to the `data` field,
4478+
// as a mutable reference to the entire `ArcInner` would assert uniqueness over the
4479+
// ref count fields too, invalidating any attempt by `Weak`s to access the ref count.
42394480
unsafe { &mut (*self.ptr.as_ptr()).data }
42404481
}
42414482
}
42424483

4484+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4485+
// #[unstable(feature = "deref_pure_trait", issue = "87121")]
4486+
unsafe impl<T: ?Sized, A: Allocator> DerefPure for UniqueArc<T, A> {}
4487+
42434488
#[unstable(feature = "unique_rc_arc", issue = "112566")]
42444489
unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for UniqueArc<T, A> {
42454490
fn drop(&mut self) {

0 commit comments

Comments
 (0)