diff options
| author | bors <bors@rust-lang.org> | 2015-12-30 19:37:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-12-30 19:37:53 +0000 |
| commit | 682cfc5187e018467f28b6bf351c9d7dd156691b (patch) | |
| tree | 3cffa33da3257d8f0d0187b6a24f54849d4fbefc /src/liballoc | |
| parent | efb5a9a9f03016b8d3d3c13f940bbbfeac2cdfa6 (diff) | |
| parent | 5b3bdafb9649b8cbaf62535c12dbfee6e9e073be (diff) | |
| download | rust-682cfc5187e018467f28b6bf351c9d7dd156691b.tar.gz rust-682cfc5187e018467f28b6bf351c9d7dd156691b.zip | |
Auto merge of #30467 - shahn:master, r=brson
This adds a constructor for a Weak that can never be upgraded. These are mostly useless, but for example are required when deserializing.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/arc.rs | 36 | ||||
| -rw-r--r-- | src/liballoc/rc.rs | 39 |
2 files changed, 74 insertions, 1 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 755e44899fc..1feac7fdf7e 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -79,6 +79,7 @@ use core::cmp::Ordering; use core::mem::{align_of_val, size_of_val}; use core::intrinsics::abort; use core::mem; +use core::mem::uninitialized; use core::ops::Deref; use core::ops::CoerceUnsized; use core::ptr::{self, Shared}; @@ -904,6 +905,35 @@ impl<T> From<T> for Arc<T> { } } +impl<T> Weak<T> { + /// Constructs a new `Weak<T>` without an accompanying instance of T. + /// + /// This allocates memory for T, but does not initialize it. Calling + /// Weak<T>::upgrade() on the return value always gives None. + /// + /// # Examples + /// + /// ``` + /// #![feature(downgraded_weak)] + /// + /// use std::sync::Weak; + /// + /// let empty: Weak<i64> = Weak::new(); + /// ``` + #[unstable(feature = "downgraded_weak", + reason = "recently added", + issue = "30425")] + pub fn new() -> Weak<T> { + unsafe { + Weak { _ptr: Shared::new(Box::into_raw(box ArcInner { + strong: atomic::AtomicUsize::new(0), + weak: atomic::AtomicUsize::new(1), + data: uninitialized(), + }))} + } + } +} + #[cfg(test)] mod tests { use std::clone::Clone; @@ -1154,6 +1184,12 @@ mod tests { let foo_arc = Arc::from(foo); assert!(123 == *foo_arc); } + + #[test] + fn test_new_weak() { + let foo: Weak<usize> = Weak::new(); + assert!(foo.upgrade().is_none()); + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 52f035b67bd..ba61a37456a 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -163,7 +163,7 @@ use core::hash::{Hasher, Hash}; use core::intrinsics::{assume, abort}; use core::marker; use core::marker::Unsize; -use core::mem::{self, align_of_val, size_of_val, forget}; +use core::mem::{self, align_of_val, size_of_val, forget, uninitialized}; use core::ops::Deref; use core::ops::CoerceUnsized; use core::ptr::{self, Shared}; @@ -824,6 +824,37 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> { } } +impl<T> Weak<T> { + /// Constructs a new `Weak<T>` without an accompanying instance of T. + /// + /// This allocates memory for T, but does not initialize it. Calling + /// Weak<T>::upgrade() on the return value always gives None. + /// + /// # Examples + /// + /// ``` + /// #![feature(downgraded_weak)] + /// + /// use std::rc::Weak; + /// + /// let empty: Weak<i64> = Weak::new(); + /// ``` + #[unstable(feature = "downgraded_weak", + reason = "recently added", + issue="30425")] + pub fn new() -> Weak<T> { + unsafe { + Weak { + _ptr: Shared::new(Box::into_raw(box RcBox { + strong: Cell::new(0), + weak: Cell::new(1), + value: uninitialized(), + })), + } + } + } +} + // NOTE: We checked_add here to deal with mem::forget safety. In particular // if you mem::forget Rcs (or Weaks), the ref-count can overflow, and then // you can free the allocation while outstanding Rcs (or Weaks) exist. @@ -1116,6 +1147,12 @@ mod tests { let foo_rc = Rc::from(foo); assert!(123 == *foo_rc); } + + #[test] + fn test_new_weak() { + let foo: Weak<usize> = Weak::new(); + assert!(foo.upgrade().is_none()); + } } #[stable(feature = "rust1", since = "1.0.0")] |
