diff options
| author | pravic <[email protected]> | 2016-06-06 23:07:53 +0300 |
|---|---|---|
| committer | pravic <[email protected]> | 2016-06-06 23:07:53 +0300 |
| commit | f2db0929feeb53567655dbdebba7e6b1c3f2f69e (patch) | |
| tree | c2cf041f838782f9ddd8994146f52e8f498bfe07 /liballoc/arc.rs | |
| parent | add 'netio' native import library (diff) | |
| parent | Merge branch 'nofp_patch' into libcore_nofp (diff) | |
| download | kmd-env-rs-master.tar.xz kmd-env-rs-master.zip | |
Diffstat (limited to 'liballoc/arc.rs')
| -rw-r--r-- | liballoc/arc.rs | 73 |
1 files changed, 40 insertions, 33 deletions
diff --git a/liballoc/arc.rs b/liballoc/arc.rs index 4aba567..729d921 100644 --- a/liballoc/arc.rs +++ b/liballoc/arc.rs @@ -72,7 +72,7 @@ use boxed::Box; use core::sync::atomic; -use core::sync::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst}; +use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; use core::borrow; use core::fmt; use core::cmp::Ordering; @@ -85,7 +85,7 @@ use core::ops::CoerceUnsized; use core::ptr::{self, Shared}; use core::marker::Unsize; use core::hash::{Hash, Hasher}; -use core::{usize, isize}; +use core::{isize, usize}; use core::convert::From; use heap::deallocate; @@ -592,6 +592,33 @@ impl<T: ?Sized> Drop 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 + /// + /// ``` + /// use std::sync::Weak; + /// + /// let empty: Weak<i64> = Weak::new(); + /// ``` + #[stable(feature = "downgraded_weak", since = "1.10.0")] + 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(), + })), + } + } + } +} + impl<T: ?Sized> Weak<T> { /// Upgrades a weak reference to a strong reference. /// @@ -630,7 +657,9 @@ impl<T: ?Sized> Weak<T> { // See comments in `Arc::clone` for why we do this (for `mem::forget`). if n > MAX_REFCOUNT { - unsafe { abort(); } + unsafe { + abort(); + } } // Relaxed is valid for the same reason it is on Arc's Clone impl @@ -682,6 +711,13 @@ impl<T: ?Sized> Clone for Weak<T> { } } +#[stable(feature = "downgraded_weak", since = "1.10.0")] +impl<T> Default for Weak<T> { + fn default() -> Weak<T> { + Weak::new() + } +} + #[stable(feature = "arc_weak", since = "1.4.0")] impl<T: ?Sized> Drop for Weak<T> { /// Drops the `Weak<T>`. @@ -907,35 +943,6 @@ 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; @@ -943,7 +950,7 @@ mod tests { use std::mem::drop; use std::ops::Drop; use std::option::Option; - use std::option::Option::{Some, None}; + use std::option::Option::{None, Some}; use std::sync::atomic; use std::sync::atomic::Ordering::{Acquire, SeqCst}; use std::thread; |