@@ -4073,7 +4073,7 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
4073
4073
/// reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
4074
4074
/// references will fail unless the `UniqueArc` they point to has been converted into a regular `Arc`.
4075
4075
///
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
4077
4077
/// use case is to have an object be mutable during its initialization phase but then have it become
4078
4078
/// immutable and converted to a normal `Arc`.
4079
4079
///
@@ -4084,7 +4084,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
4084
4084
/// use std::sync::{Arc, Weak, UniqueArc};
4085
4085
///
4086
4086
/// struct Gadget {
4087
- /// #[allow(dead_code)]
4088
4087
/// me: Weak<Gadget>,
4089
4088
/// }
4090
4089
///
@@ -4104,7 +4103,6 @@ impl<T: core::error::Error + ?Sized> core::error::Error for Arc<T> {
4104
4103
/// previous example, `UniqueArc` allows for more flexibility in the construction of cyclic data,
4105
4104
/// including fallible or async constructors.
4106
4105
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4107
- #[ derive( Debug ) ]
4108
4106
pub struct UniqueArc <
4109
4107
T : ?Sized ,
4110
4108
#[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
@@ -4119,13 +4117,248 @@ pub struct UniqueArc<
4119
4117
}
4120
4118
4121
4119
#[ 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")]
4122
4127
impl < T : ?Sized + Unsize < U > , U : ?Sized , A : Allocator > CoerceUnsized < UniqueArc < U , A > >
4123
4128
for UniqueArc < T , A >
4124
4129
{
4125
4130
}
4126
4131
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 > {
4129
4362
/// Creates a new `UniqueArc`.
4130
4363
///
4131
4364
/// Weak references to this `UniqueArc` can be created with [`UniqueArc::downgrade`]. Upgrading
@@ -4134,6 +4367,7 @@ impl<T> UniqueArc<T> {
4134
4367
/// point to the new [`Arc`].
4135
4368
#[ cfg( not( no_global_oom_handling) ) ]
4136
4369
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4370
+ #[ must_use]
4137
4371
pub fn new ( value : T ) -> Self {
4138
4372
Self :: new_in ( value, Global )
4139
4373
}
@@ -4148,6 +4382,8 @@ impl<T, A: Allocator> UniqueArc<T, A> {
4148
4382
/// point to the new [`Arc`].
4149
4383
#[ cfg( not( no_global_oom_handling) ) ]
4150
4384
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4385
+ #[ must_use]
4386
+ // #[unstable(feature = "allocator_api", issue = "32838")]
4151
4387
pub fn new_in ( data : T , alloc : A ) -> Self {
4152
4388
let ( ptr, alloc) = Box :: into_unique ( Box :: new_in (
4153
4389
ArcInner {
@@ -4172,6 +4408,7 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
4172
4408
/// Any weak references created before this method is called can now be upgraded to strong
4173
4409
/// references.
4174
4410
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4411
+ #[ must_use]
4175
4412
pub fn into_arc ( this : Self ) -> Arc < T , A > {
4176
4413
let this = ManuallyDrop :: new ( this) ;
4177
4414
@@ -4195,6 +4432,7 @@ impl<T: ?Sized, A: Allocator + Clone> UniqueArc<T, A> {
4195
4432
/// Attempting to upgrade this weak reference will fail before the `UniqueArc` has been converted
4196
4433
/// to a [`Arc`] using [`UniqueArc::into_arc`].
4197
4434
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4435
+ #[ must_use]
4198
4436
pub fn downgrade ( this : & Self ) -> Weak < T , A > {
4199
4437
// Using a relaxed ordering is alright here, as knowledge of the
4200
4438
// original reference prevents other threads from erroneously deleting
@@ -4236,10 +4474,17 @@ impl<T: ?Sized, A: Allocator> DerefMut for UniqueArc<T, A> {
4236
4474
// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we
4237
4475
// have unique ownership and therefore it's safe to make a mutable reference because
4238
4476
// `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.
4239
4480
unsafe { & mut ( * self . ptr . as_ptr ( ) ) . data }
4240
4481
}
4241
4482
}
4242
4483
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
+
4243
4488
#[ unstable( feature = "unique_rc_arc" , issue = "112566" ) ]
4244
4489
unsafe impl < #[ may_dangle] T : ?Sized , A : Allocator > Drop for UniqueArc < T , A > {
4245
4490
fn drop ( & mut self ) {
0 commit comments