Skip to content

Commit 0b2c03e

Browse files
committed
Add more APIs for UniqueArc
1 parent 9310c96 commit 0b2c03e

File tree

1 file changed

+246
-5
lines changed

1 file changed

+246
-5
lines changed

library/alloc/src/sync.rs

+246-5
Original file line numberDiff line numberDiff line change
@@ -4034,7 +4034,7 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
40344034
/// reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
40354035
/// references will fail unless the `UniqueArc` they point to has been converted into a regular `Arc`.
40364036
///
4037-
/// Because they are uniquely owned, the contents of a `UniqueArc` can be freely mutated. A common
4037+
/// Because it is uniquely owned, the contents of a `UniqueArc` can be freely mutated. A common
40384038
/// use case is to have an object be mutable during its initialization phase but then have it become
40394039
/// immutable and converted to a normal `Arc`.
40404040
///
@@ -4045,7 +4045,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
40454045
/// use std::sync::{Arc, Weak, UniqueArc};
40464046
///
40474047
/// struct Gadget {
4048-
/// #[allow(dead_code)]
40494048
/// me: Weak<Gadget>,
40504049
/// }
40514050
///
@@ -4065,7 +4064,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
40654064
/// previous example, `UniqueArc` allows for more flexibility in the construction of cyclic data,
40664065
/// including fallible or async constructors.
40674066
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4068-
#[derive(Debug)]
40694067
pub struct UniqueArc<
40704068
T: ?Sized,
40714069
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
@@ -4080,13 +4078,248 @@ pub struct UniqueArc<
40804078
}
40814079

40824080
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4081+
unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Send> Send for UniqueArc<T, A> {}
4082+
4083+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4084+
unsafe impl<T: ?Sized + Sync + Send, A: Allocator + Sync> Sync for UniqueArc<T, A> {}
4085+
4086+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4087+
// #[unstable(feature = "coerce_unsized", issue = "18598")]
40834088
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<UniqueArc<U, A>>
40844089
for UniqueArc<T, A>
40854090
{
40864091
}
40874092

4088-
// Depends on A = Global
4089-
impl<T> UniqueArc<T> {
4093+
//#[unstable(feature = "unique_rc_arc", issue = "112566")]
4094+
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
4095+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<UniqueArc<U>> for UniqueArc<T> {}
4096+
4097+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4098+
impl<T: ?Sized + fmt::Display, A: Allocator> fmt::Display for UniqueArc<T, A> {
4099+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4100+
fmt::Display::fmt(&**self, f)
4101+
}
4102+
}
4103+
4104+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4105+
impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for UniqueArc<T, A> {
4106+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4107+
fmt::Debug::fmt(&**self, f)
4108+
}
4109+
}
4110+
4111+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4112+
impl<T: ?Sized, A: Allocator> fmt::Pointer for UniqueArc<T, A> {
4113+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4114+
fmt::Pointer::fmt(&(&raw const **self), f)
4115+
}
4116+
}
4117+
4118+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4119+
impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for UniqueArc<T, A> {
4120+
fn borrow(&self) -> &T {
4121+
&**self
4122+
}
4123+
}
4124+
4125+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4126+
impl<T: ?Sized, A: Allocator> borrow::BorrowMut<T> for UniqueArc<T, A> {
4127+
fn borrow_mut(&mut self) -> &mut T {
4128+
&mut **self
4129+
}
4130+
}
4131+
4132+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4133+
impl<T: ?Sized, A: Allocator> AsRef<T> for UniqueArc<T, A> {
4134+
fn as_ref(&self) -> &T {
4135+
&**self
4136+
}
4137+
}
4138+
4139+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4140+
impl<T: ?Sized, A: Allocator> AsMut<T> for UniqueArc<T, A> {
4141+
fn as_mut(&mut self) -> &mut T {
4142+
&mut **self
4143+
}
4144+
}
4145+
4146+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4147+
impl<T: ?Sized, A: Allocator> Unpin for UniqueArc<T, A> {}
4148+
4149+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4150+
impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for UniqueArc<T, A> {
4151+
/// Equality for two `UniqueArc`s.
4152+
///
4153+
/// Two `UniqueArc`s are equal if their inner values are equal.
4154+
///
4155+
/// # Examples
4156+
///
4157+
/// ```
4158+
/// #![feature(unique_rc_arc)]
4159+
/// use std::sync::UniqueArc;
4160+
///
4161+
/// let five = UniqueArc::new(5);
4162+
///
4163+
/// assert!(five == UniqueArc::new(5));
4164+
/// ```
4165+
#[inline]
4166+
fn eq(&self, other: &Self) -> bool {
4167+
PartialEq::eq(&**self, &**other)
4168+
}
4169+
4170+
/// Inequality for two `UniqueArc`s.
4171+
///
4172+
/// Two `UniqueArc`s are not equal if their inner values are not equal.
4173+
///
4174+
/// # Examples
4175+
///
4176+
/// ```
4177+
/// #![feature(unique_rc_arc)]
4178+
/// use std::sync::UniqueArc;
4179+
///
4180+
/// let five = UniqueArc::new(5);
4181+
///
4182+
/// assert!(five != UniqueArc::new(6));
4183+
/// ```
4184+
#[inline]
4185+
fn ne(&self, other: &Self) -> bool {
4186+
PartialEq::ne(&**self, &**other)
4187+
}
4188+
}
4189+
4190+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4191+
impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for UniqueArc<T, A> {
4192+
/// Partial comparison for two `UniqueArc`s.
4193+
///
4194+
/// The two are compared by calling `partial_cmp()` on their inner values.
4195+
///
4196+
/// # Examples
4197+
///
4198+
/// ```
4199+
/// #![feature(unique_rc_arc)]
4200+
/// use std::sync::UniqueArc;
4201+
/// use std::cmp::Ordering;
4202+
///
4203+
/// let five = UniqueArc::new(5);
4204+
///
4205+
/// assert_eq!(Some(Ordering::Less), five.partial_cmp(&UniqueArc::new(6)));
4206+
/// ```
4207+
#[inline(always)]
4208+
fn partial_cmp(&self, other: &UniqueArc<T, A>) -> Option<Ordering> {
4209+
(**self).partial_cmp(&**other)
4210+
}
4211+
4212+
/// Less-than comparison for two `UniqueArc`s.
4213+
///
4214+
/// The two are compared by calling `<` on their inner values.
4215+
///
4216+
/// # Examples
4217+
///
4218+
/// ```
4219+
/// #![feature(unique_rc_arc)]
4220+
/// use std::sync::UniqueArc;
4221+
///
4222+
/// let five = UniqueArc::new(5);
4223+
///
4224+
/// assert!(five < UniqueArc::new(6));
4225+
/// ```
4226+
#[inline(always)]
4227+
fn lt(&self, other: &UniqueArc<T, A>) -> bool {
4228+
**self < **other
4229+
}
4230+
4231+
/// 'Less than or equal to' comparison for two `UniqueArc`s.
4232+
///
4233+
/// The two are compared by calling `<=` on their inner values.
4234+
///
4235+
/// # Examples
4236+
///
4237+
/// ```
4238+
/// #![feature(unique_rc_arc)]
4239+
/// use std::sync::UniqueArc;
4240+
///
4241+
/// let five = UniqueArc::new(5);
4242+
///
4243+
/// assert!(five <= UniqueArc::new(5));
4244+
/// ```
4245+
#[inline(always)]
4246+
fn le(&self, other: &UniqueArc<T, A>) -> bool {
4247+
**self <= **other
4248+
}
4249+
4250+
/// Greater-than comparison for two `UniqueArc`s.
4251+
///
4252+
/// The two are compared by calling `>` on their inner values.
4253+
///
4254+
/// # Examples
4255+
///
4256+
/// ```
4257+
/// #![feature(unique_rc_arc)]
4258+
/// use std::sync::UniqueArc;
4259+
///
4260+
/// let five = UniqueArc::new(5);
4261+
///
4262+
/// assert!(five > UniqueArc::new(4));
4263+
/// ```
4264+
#[inline(always)]
4265+
fn gt(&self, other: &UniqueArc<T, A>) -> bool {
4266+
**self > **other
4267+
}
4268+
4269+
/// 'Greater than or equal to' comparison for two `UniqueArc`s.
4270+
///
4271+
/// The two are compared by calling `>=` on their inner values.
4272+
///
4273+
/// # Examples
4274+
///
4275+
/// ```
4276+
/// #![feature(unique_rc_arc)]
4277+
/// use std::sync::UniqueArc;
4278+
///
4279+
/// let five = UniqueArc::new(5);
4280+
///
4281+
/// assert!(five >= UniqueArc::new(5));
4282+
/// ```
4283+
#[inline(always)]
4284+
fn ge(&self, other: &UniqueArc<T, A>) -> bool {
4285+
**self >= **other
4286+
}
4287+
}
4288+
4289+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4290+
impl<T: ?Sized + Ord, A: Allocator> Ord for UniqueArc<T, A> {
4291+
/// Comparison for two `UniqueArc`s.
4292+
///
4293+
/// The two are compared by calling `cmp()` on their inner values.
4294+
///
4295+
/// # Examples
4296+
///
4297+
/// ```
4298+
/// #![feature(unique_rc_arc)]
4299+
/// use std::sync::UniqueArc;
4300+
/// use std::cmp::Ordering;
4301+
///
4302+
/// let five = UniqueArc::new(5);
4303+
///
4304+
/// assert_eq!(Ordering::Less, five.cmp(&UniqueArc::new(6)));
4305+
/// ```
4306+
#[inline]
4307+
fn cmp(&self, other: &UniqueArc<T, A>) -> Ordering {
4308+
(**self).cmp(&**other)
4309+
}
4310+
}
4311+
4312+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4313+
impl<T: ?Sized + Eq, A: Allocator> Eq for UniqueArc<T, A> {}
4314+
4315+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4316+
impl<T: ?Sized + Hash, A: Allocator> Hash for UniqueArc<T, A> {
4317+
fn hash<H: Hasher>(&self, state: &mut H) {
4318+
(**self).hash(state);
4319+
}
4320+
}
4321+
4322+
impl<T> UniqueArc<T, Global> {
40904323
/// Creates a new `UniqueArc`.
40914324
///
40924325
/// Weak references to this `UniqueArc` can be created with [`UniqueArc::downgrade`]. Upgrading
@@ -4095,6 +4328,7 @@ impl<T> UniqueArc<T> {
40954328
/// point to the new [`Arc`].
40964329
#[cfg(not(no_global_oom_handling))]
40974330
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4331+
#[must_use]
40984332
pub fn new(value: T) -> Self {
40994333
Self::new_in(value, Global)
41004334
}
@@ -4109,6 +4343,8 @@ impl<T, A: Allocator> UniqueArc<T, A> {
41094343
/// point to the new [`Arc`].
41104344
#[cfg(not(no_global_oom_handling))]
41114345
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4346+
#[must_use]
4347+
// #[unstable(feature = "allocator_api", issue = "32838")]
41124348
pub fn new_in(data: T, alloc: A) -> Self {
41134349
let (ptr, alloc) = Box::into_unique(Box::new_in(
41144350
ArcInner {
@@ -4133,6 +4369,7 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
41334369
/// Any weak references created before this method is called can now be upgraded to strong
41344370
/// references.
41354371
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4372+
#[must_use]
41364373
pub fn into_arc(this: Self) -> Arc<T, A> {
41374374
let this = ManuallyDrop::new(this);
41384375

@@ -4156,6 +4393,7 @@ impl<T: ?Sized, A: Allocator + Clone> UniqueArc<T, A> {
41564393
/// Attempting to upgrade this weak reference will fail before the `UniqueArc` has been converted
41574394
/// to a [`Arc`] using [`UniqueArc::into_arc`].
41584395
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4396+
#[must_use]
41594397
pub fn downgrade(this: &Self) -> Weak<T, A> {
41604398
// Using a relaxed ordering is alright here, as knowledge of the
41614399
// original reference prevents other threads from erroneously deleting
@@ -4197,6 +4435,9 @@ impl<T: ?Sized, A: Allocator> DerefMut for UniqueArc<T, A> {
41974435
// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we
41984436
// have unique ownership and therefore it's safe to make a mutable reference because
41994437
// `UniqueArc` owns the only strong reference to itself.
4438+
// We also need to be careful to only create a mutable reference to the `data` field,
4439+
// as a mutable reference to the entire `ArcInner` would assert uniqueness over the
4440+
// ref count fields too, invalidating any attempt by `Weak`s to access the ref count.
42004441
unsafe { &mut (*self.ptr.as_ptr()).data }
42014442
}
42024443
}

0 commit comments

Comments
 (0)