]> git.lizzy.rs Git - rust.git/commitdiff
Implement Weak::new_downgraded() (#30425)
authorSebastian Hahn <sebastian@torproject.org>
Fri, 18 Dec 2015 19:40:17 +0000 (20:40 +0100)
committerSebastian Hahn <sebastian@torproject.org>
Fri, 18 Dec 2015 19:40:17 +0000 (20:40 +0100)
This adds a constructor for a Weak that can never be upgraded. These are
mostly useless, but for example are required when deserializing.

src/liballoc/rc.rs

index 8f00605d33b37c0da1f8d391ec5795ce31528486..15debf2683d1f598d3ec36707849e2653dda780a 100644 (file)
 use core::cmp::Ordering;
 use core::fmt;
 use core::hash::{Hasher, Hash};
-use core::intrinsics::{assume, abort};
+use core::intrinsics::{assume, abort, uninit};
 use core::marker;
 #[cfg(not(stage0))]
 use core::marker::Unsize;
@@ -830,6 +830,36 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
+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::rc::Weak;
+    ///
+    /// let five = Weak::new_downgraded();
+    /// ```
+
+    #[unstable(feature = "downgraded_weak",
+               reason = "recently added",
+               issue="30425")]
+    pub fn new_downgraded() -> Weak<T> {
+        unsafe {
+            Weak {
+                _ptr: Shared::new(Box::into_raw(box RcBox {
+                    strong: Cell::new(0),
+                    weak: Cell::new(1),
+                    value: uninit(),
+                })),
+            }
+        }
+    }
+}
+
 // 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.
@@ -1122,6 +1152,12 @@ fn test_from_owned() {
         let foo_rc = Rc::from(foo);
         assert!(123 == *foo_rc);
     }
+
+    #[test]
+    fn test_new_downgraded() {
+        let foo: Weak<usize> = Weak::new_downgraded();
+        assert!(foo.upgrade().is_none());
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]