@@ -4034,7 +4034,7 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
4034
4034
/// reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
4035
4035
/// references will fail unless the `UniqueArc` they point to has been converted into a regular `Arc`.
4036
4036
///
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
4038
4038
/// use case is to have an object be mutable during its initialization phase but then have it become
4039
4039
/// immutable and converted to a normal `Arc`.
4040
4040
///
@@ -4045,7 +4045,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
4045
4045
/// use std::sync::{Arc, Weak, UniqueArc};
4046
4046
///
4047
4047
/// struct Gadget {
4048
- /// #[allow(dead_code)]
4049
4048
/// me: Weak<Gadget>,
4050
4049
/// }
4051
4050
///
@@ -4065,7 +4064,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
4065
4064
/// previous example, `UniqueArc` allows for more flexibility in the construction of cyclic data,
4066
4065
/// including fallible or async constructors.
4067
4066
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4068
- #[ derive( Debug ) ]
4069
4067
pub struct UniqueArc <
4070
4068
T : ?Sized ,
4071
4069
#[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
@@ -4080,13 +4078,248 @@ pub struct UniqueArc<
4080
4078
}
4081
4079
4082
4080
#[ 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")]
4083
4088
impl < T : ?Sized + Unsize < U > , U : ?Sized , A : Allocator > CoerceUnsized < UniqueArc < U , A > >
4084
4089
for UniqueArc < T , A >
4085
4090
{
4086
4091
}
4087
4092
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 > {
4090
4323
/// Creates a new `UniqueArc`.
4091
4324
///
4092
4325
/// Weak references to this `UniqueArc` can be created with [`UniqueArc::downgrade`]. Upgrading
@@ -4095,6 +4328,7 @@ impl<T> UniqueArc<T> {
4095
4328
/// point to the new [`Arc`].
4096
4329
#[ cfg( not( no_global_oom_handling) ) ]
4097
4330
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4331
+ #[ must_use]
4098
4332
pub fn new ( value : T ) -> Self {
4099
4333
Self :: new_in ( value, Global )
4100
4334
}
@@ -4109,6 +4343,8 @@ impl<T, A: Allocator> UniqueArc<T, A> {
4109
4343
/// point to the new [`Arc`].
4110
4344
#[ cfg( not( no_global_oom_handling) ) ]
4111
4345
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4346
+ #[ must_use]
4347
+ // #[unstable(feature = "allocator_api", issue = "32838")]
4112
4348
pub fn new_in ( data : T , alloc : A ) -> Self {
4113
4349
let ( ptr, alloc) = Box :: into_unique ( Box :: new_in (
4114
4350
ArcInner {
@@ -4133,6 +4369,7 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
4133
4369
/// Any weak references created before this method is called can now be upgraded to strong
4134
4370
/// references.
4135
4371
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4372
+ #[ must_use]
4136
4373
pub fn into_arc ( this : Self ) -> Arc < T , A > {
4137
4374
let this = ManuallyDrop :: new ( this) ;
4138
4375
@@ -4156,6 +4393,7 @@ impl<T: ?Sized, A: Allocator + Clone> UniqueArc<T, A> {
4156
4393
/// Attempting to upgrade this weak reference will fail before the `UniqueArc` has been converted
4157
4394
/// to a [`Arc`] using [`UniqueArc::into_arc`].
4158
4395
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4396
+ #[ must_use]
4159
4397
pub fn downgrade ( this : & Self ) -> Weak < T , A > {
4160
4398
// Using a relaxed ordering is alright here, as knowledge of the
4161
4399
// original reference prevents other threads from erroneously deleting
@@ -4197,6 +4435,9 @@ impl<T: ?Sized, A: Allocator> DerefMut for UniqueArc<T, A> {
4197
4435
// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we
4198
4436
// have unique ownership and therefore it's safe to make a mutable reference because
4199
4437
// `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.
4200
4441
unsafe { & mut ( * self . ptr . as_ptr ( ) ) . data }
4201
4442
}
4202
4443
}
0 commit comments