@@ -220,6 +220,42 @@ impl<T> Rc<T> {
220
220
}
221
221
}
222
222
}
223
+
224
+ /// Unwraps the contained value if the `Rc<T>` is unique.
225
+ ///
226
+ /// If the `Rc<T>` is not unique, an `Err` is returned with the same
227
+ /// `Rc<T>`.
228
+ ///
229
+ /// # Examples
230
+ ///
231
+ /// ```
232
+ /// # #![feature(rc_unique)]
233
+ /// use std::rc::{self, Rc};
234
+ ///
235
+ /// let x = Rc::new(3);
236
+ /// assert_eq!(rc::try_unwrap(x), Ok(3));
237
+ ///
238
+ /// let x = Rc::new(4);
239
+ /// let _y = x.clone();
240
+ /// assert_eq!(rc::try_unwrap(x), Err(Rc::new(4)));
241
+ /// ```
242
+ #[ inline]
243
+ #[ unstable( feature = "rc_unique" ) ]
244
+ pub fn try_unwrap ( rc : Rc < T > ) -> Result < T , Rc < T > > {
245
+ if Rc :: is_unique ( & rc) {
246
+ unsafe {
247
+ let val = ptr:: read ( & * rc) ; // copy the contained object
248
+ // destruct the box and skip our Drop
249
+ // we can ignore the refcounts because we know we're unique
250
+ deallocate ( * rc. _ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
251
+ min_align_of :: < RcBox < T > > ( ) ) ;
252
+ forget ( rc) ;
253
+ Ok ( val)
254
+ }
255
+ } else {
256
+ Err ( rc)
257
+ }
258
+ }
223
259
}
224
260
225
261
impl < T : ?Sized > Rc < T > {
@@ -241,17 +277,78 @@ impl<T: ?Sized> Rc<T> {
241
277
self . inc_weak ( ) ;
242
278
Weak { _ptr : self . _ptr }
243
279
}
280
+
281
+ /// Get the number of weak references to this value.
282
+ #[ inline]
283
+ #[ unstable( feature = "rc_counts" ) ]
284
+ pub fn weak_count ( this : & Rc < T > ) -> usize { this. weak ( ) - 1 }
285
+
286
+ /// Get the number of strong references to this value.
287
+ #[ inline]
288
+ #[ unstable( feature = "rc_counts" ) ]
289
+ pub fn strong_count ( this : & Rc < T > ) -> usize { this. strong ( ) }
290
+
291
+ /// Returns true if there are no other `Rc` or `Weak<T>` values that share
292
+ /// the same inner value.
293
+ ///
294
+ /// # Examples
295
+ ///
296
+ /// ```
297
+ /// # #![feature(rc_unique)]
298
+ /// use std::rc;
299
+ /// use std::rc::Rc;
300
+ ///
301
+ /// let five = Rc::new(5);
302
+ ///
303
+ /// rc::is_unique(&five);
304
+ /// ```
305
+ #[ inline]
306
+ #[ unstable( feature = "rc_unique" ) ]
307
+ pub fn is_unique ( rc : & Rc < T > ) -> bool {
308
+ weak_count ( rc) == 0 && strong_count ( rc) == 1
309
+ }
310
+
311
+ /// Returns a mutable reference to the contained value if the `Rc<T>` is
312
+ /// unique.
313
+ ///
314
+ /// Returns `None` if the `Rc<T>` is not unique.
315
+ ///
316
+ /// # Examples
317
+ ///
318
+ /// ```
319
+ /// # #![feature(rc_unique)]
320
+ /// use std::rc::{self, Rc};
321
+ ///
322
+ /// let mut x = Rc::new(3);
323
+ /// *rc::get_mut(&mut x).unwrap() = 4;
324
+ /// assert_eq!(*x, 4);
325
+ ///
326
+ /// let _y = x.clone();
327
+ /// assert!(rc::get_mut(&mut x).is_none());
328
+ /// ```
329
+ #[ inline]
330
+ #[ unstable( feature = "rc_unique" ) ]
331
+ pub fn get_mut ( rc : & mut Rc < T > ) -> Option < & mut T > {
332
+ if Rc :: is_unique ( rc) {
333
+ let inner = unsafe { & mut * * rc. _ptr } ;
334
+ Some ( & mut inner. value )
335
+ } else {
336
+ None
337
+ }
338
+ }
244
339
}
245
340
246
341
/// Get the number of weak references to this value.
247
342
#[ inline]
248
343
#[ unstable( feature = "rc_counts" ) ]
249
- pub fn weak_count < T : ?Sized > ( this : & Rc < T > ) -> usize { this. weak ( ) - 1 }
344
+ #[ deprecated( since = "1.2.0" , reason = "renamed to Rc::weak_count" ) ]
345
+ pub fn weak_count < T : ?Sized > ( this : & Rc < T > ) -> usize { Rc :: weak_count ( this) }
250
346
251
347
/// Get the number of strong references to this value.
252
348
#[ inline]
253
349
#[ unstable( feature = "rc_counts" ) ]
254
- pub fn strong_count < T : ?Sized > ( this : & Rc < T > ) -> usize { this. strong ( ) }
350
+ #[ deprecated( since = "1.2.0" , reason = "renamed to Rc::strong_count" ) ]
351
+ pub fn strong_count < T : ?Sized > ( this : & Rc < T > ) -> usize { Rc :: strong_count ( this) }
255
352
256
353
/// Returns true if there are no other `Rc` or `Weak<T>` values that share the
257
354
/// same inner value.
@@ -269,9 +366,8 @@ pub fn strong_count<T: ?Sized>(this: &Rc<T>) -> usize { this.strong() }
269
366
/// ```
270
367
#[ inline]
271
368
#[ unstable( feature = "rc_unique" ) ]
272
- pub fn is_unique < T > ( rc : & Rc < T > ) -> bool {
273
- weak_count ( rc) == 0 && strong_count ( rc) == 1
274
- }
369
+ #[ deprecated( since = "1.2.0" , reason = "renamed to Rc::is_unique" ) ]
370
+ pub fn is_unique < T > ( rc : & Rc < T > ) -> bool { Rc :: is_unique ( rc) }
275
371
276
372
/// Unwraps the contained value if the `Rc<T>` is unique.
277
373
///
@@ -292,21 +388,8 @@ pub fn is_unique<T>(rc: &Rc<T>) -> bool {
292
388
/// ```
293
389
#[ inline]
294
390
#[ unstable( feature = "rc_unique" ) ]
295
- pub fn try_unwrap < T > ( rc : Rc < T > ) -> Result < T , Rc < T > > {
296
- if is_unique ( & rc) {
297
- unsafe {
298
- let val = ptr:: read ( & * rc) ; // copy the contained object
299
- // destruct the box and skip our Drop
300
- // we can ignore the refcounts because we know we're unique
301
- deallocate ( * rc. _ptr as * mut u8 , size_of :: < RcBox < T > > ( ) ,
302
- min_align_of :: < RcBox < T > > ( ) ) ;
303
- forget ( rc) ;
304
- Ok ( val)
305
- }
306
- } else {
307
- Err ( rc)
308
- }
309
- }
391
+ #[ deprecated( since = "1.2.0" , reason = "renamed to Rc::try_unwrap" ) ]
392
+ pub fn try_unwrap < T > ( rc : Rc < T > ) -> Result < T , Rc < T > > { Rc :: try_unwrap ( rc) }
310
393
311
394
/// Returns a mutable reference to the contained value if the `Rc<T>` is unique.
312
395
///
@@ -327,14 +410,8 @@ pub fn try_unwrap<T>(rc: Rc<T>) -> Result<T, Rc<T>> {
327
410
/// ```
328
411
#[ inline]
329
412
#[ unstable( feature = "rc_unique" ) ]
330
- pub fn get_mut < T > ( rc : & mut Rc < T > ) -> Option < & mut T > {
331
- if is_unique ( rc) {
332
- let inner = unsafe { & mut * * rc. _ptr } ;
333
- Some ( & mut inner. value )
334
- } else {
335
- None
336
- }
337
- }
413
+ #[ deprecated( since = "1.2.0" , reason = "renamed to Rc::get_mut" ) ]
414
+ pub fn get_mut < T > ( rc : & mut Rc < T > ) -> Option < & mut T > { Rc :: get_mut ( rc) }
338
415
339
416
impl < T : Clone > Rc < T > {
340
417
/// Make a mutable reference from the given `Rc<T>`.
0 commit comments