diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-03-12 20:44:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-12 20:44:50 +0100 |
| commit | b28775cdcb718f9b3455b7661b3a7ae3f63f6511 (patch) | |
| tree | 20f616e79d75af18b80458fb922cdec26875577e /library/alloc | |
| parent | afe257554c30693f545c50fd864b137023deb76a (diff) | |
| parent | 992957efa9a4f12641ffb229e3b57c7088d140eb (diff) | |
| download | rust-b28775cdcb718f9b3455b7661b3a7ae3f63f6511.tar.gz rust-b28775cdcb718f9b3455b7661b3a7ae3f63f6511.zip | |
Rollup merge of #109026 - joshtriplett:rc-into-inner, r=dtolnay
Introduce `Rc::into_inner`, as a parallel to `Arc::into_inner` Unlike `Arc`, `Rc` doesn't have the same race condition to avoid, but maintaining an equivalent API still makes it easier to work with both `Rc` and `Arc`.
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/rc.rs | 18 | ||||
| -rw-r--r-- | library/alloc/src/rc/tests.rs | 15 |
2 files changed, 33 insertions, 0 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 932a537c598..77b0447b345 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -681,6 +681,24 @@ impl<T> Rc<T> { Err(this) } } + + /// Returns the inner value, if the `Rc` has exactly one strong reference. + /// + /// Otherwise, [`None`] is returned and the `Rc` is dropped. + /// + /// This will succeed even if there are outstanding weak references. + /// + /// If `Rc::into_inner` is called on every clone of this `Rc`, + /// it is guaranteed that exactly one of the calls returns the inner value. + /// This means in particular that the inner value is not dropped. + /// + /// This is equivalent to `Rc::try_unwrap(...).ok()`. (Note that these are not equivalent for + /// `Arc`, due to race conditions that do not apply to `Rc`.) + #[inline] + #[unstable(feature = "rc_into_inner", issue = "106894")] + pub fn into_inner(this: Self) -> Option<T> { + Rc::try_unwrap(this).ok() + } } impl<T> Rc<[T]> { diff --git a/library/alloc/src/rc/tests.rs b/library/alloc/src/rc/tests.rs index 32433cfbdcf..2784108e0e6 100644 --- a/library/alloc/src/rc/tests.rs +++ b/library/alloc/src/rc/tests.rs @@ -152,6 +152,21 @@ fn try_unwrap() { } #[test] +fn into_inner() { + let x = Rc::new(3); + assert_eq!(Rc::into_inner(x), Some(3)); + + let x = Rc::new(4); + let y = Rc::clone(&x); + assert_eq!(Rc::into_inner(x), None); + assert_eq!(Rc::into_inner(y), Some(4)); + + let x = Rc::new(5); + let _w = Rc::downgrade(&x); + assert_eq!(Rc::into_inner(x), Some(5)); +} + +#[test] fn into_from_raw() { let x = Rc::new(Box::new("hello")); let y = x.clone(); |
