about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorSebastian Hahn <sebastian@torproject.org>2015-12-18 23:32:16 +0100
committerSebastian Hahn <sebastian@torproject.org>2015-12-19 00:23:07 +0100
commit9697076b62553fe8ca3dc23d5cc067a94d9e4570 (patch)
treeaf8869eee7ced44315f59905d083faadbca40b2e /src/liballoc
parent4741ad38b59d633144ccdfefd3398ec7cf85b3cc (diff)
downloadrust-9697076b62553fe8ca3dc23d5cc067a94d9e4570.tar.gz
rust-9697076b62553fe8ca3dc23d5cc067a94d9e4570.zip
Implement arc::Weak::new()
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/arc.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs
index 9479d47943e..377dfb11f92 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;
 #[cfg(not(stage0))]
 use core::ops::CoerceUnsized;
@@ -910,6 +911,36 @@ 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::Arc;
+    ///
+    /// let five = Arc::new(5);
+    /// ```
+    #[unstable(feature = "downgraded_weak",
+               reason = "recently added",
+               issue = "30425")]
+    pub fn new() -> Weak<T> {
+        unsafe {
+            let x: Box<_> = box ArcInner {
+                strong: atomic::AtomicUsize::new(0),
+                weak: atomic::AtomicUsize::new(1),
+                data: uninitialized(),
+            };
+            Weak { _ptr: Shared::new(Box::into_raw(x)) }
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use std::clone::Clone;
@@ -1160,6 +1191,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")]