about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorFlavio Percoco <flaper87@gmail.com>2015-01-11 11:09:53 +0100
committerFlavio Percoco <flaper87@gmail.com>2015-01-16 08:18:56 +0100
commitbb04121138b636f6aa43e10fa38629043a0f0f48 (patch)
treea2ed347e2dfb1ebf6d31b7b8c39f53a1bb3e65f6 /src/liballoc
parent388e30f78e545deb798f8cd6192cf939e5d8342a (diff)
downloadrust-bb04121138b636f6aa43e10fa38629043a0f0f48.tar.gz
rust-bb04121138b636f6aa43e10fa38629043a0f0f48.zip
Don't use NoSend/NoSync in liballoc
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/lib.rs1
-rw-r--r--src/liballoc/rc.rs159
2 files changed, 160 insertions, 0 deletions
diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs
index 4a85637625a..6c853306035 100644
--- a/src/liballoc/lib.rs
+++ b/src/liballoc/lib.rs
@@ -68,6 +68,7 @@
 #![allow(unknown_features)]
 #![feature(lang_items, unsafe_destructor)]
 #![feature(box_syntax)]
+#![feature(optin_builtin_traits)]
 #![allow(unknown_features)] #![feature(int_uint)]
 
 #[macro_use]
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index f42c6dbdc15..0e18cdda8dd 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -174,6 +174,7 @@ struct RcBox<T> {
 /// See the [module level documentation](../index.html) for more details.
 #[unsafe_no_drop_flag]
 #[stable]
+#[cfg(stage0)] // NOTE remove impl after next snapshot
 pub struct Rc<T> {
     // FIXME #12808: strange names to try to avoid interfering with field accesses of the contained
     // type via Deref
@@ -182,6 +183,24 @@ pub struct Rc<T> {
     _noshare: marker::NoSync
 }
 
+/// An immutable reference-counted pointer type.
+///
+/// See the [module level documentation](../index.html) for more details.
+#[unsafe_no_drop_flag]
+#[stable]
+#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+pub struct Rc<T> {
+    // FIXME #12808: strange names to try to avoid interfering with field accesses of the contained
+    // type via Deref
+    _ptr: NonZero<*mut RcBox<T>>,
+}
+
+#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+impl<T> !marker::Send for Rc<T> {}
+
+#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+impl<T> !marker::Sync for Rc<T> {}
+
 impl<T> Rc<T> {
     /// Constructs a new `Rc<T>`.
     ///
@@ -193,6 +212,7 @@ impl<T> Rc<T> {
     /// let five = Rc::new(5i);
     /// ```
     #[stable]
+    #[cfg(stage0)] // NOTE remove after next snapshot
     pub fn new(value: T) -> Rc<T> {
         unsafe {
             Rc {
@@ -210,6 +230,32 @@ impl<T> Rc<T> {
         }
     }
 
+    /// Constructs a new `Rc<T>`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::rc::Rc;
+    ///
+    /// let five = Rc::new(5i);
+    /// ```
+    #[stable]
+    #[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+    pub fn new(value: T) -> Rc<T> {
+        unsafe {
+            Rc {
+                // there is an implicit weak pointer owned by all the strong pointers, which
+                // ensures that the weak destructor never frees the allocation while the strong
+                // destructor is running, even if the weak pointer is stored inside the strong one.
+                _ptr: NonZero::new(transmute(box RcBox {
+                    value: value,
+                    strong: Cell::new(1),
+                    weak: Cell::new(1)
+                })),
+            }
+        }
+    }
+
     /// Downgrades the `Rc<T>` to a `Weak<T>` reference.
     ///
     /// # Examples
@@ -221,6 +267,7 @@ impl<T> Rc<T> {
     ///
     /// let weak_five = five.downgrade();
     /// ```
+    #[cfg(stage0)] // NOTE remove after next snapshot
     #[unstable = "Weak pointers may not belong in this module"]
     pub fn downgrade(&self) -> Weak<T> {
         self.inc_weak();
@@ -230,6 +277,24 @@ impl<T> Rc<T> {
             _noshare: marker::NoSync
         }
     }
+
+    /// Downgrades the `Rc<T>` to a `Weak<T>` reference.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::rc::Rc;
+    ///
+    /// let five = Rc::new(5i);
+    ///
+    /// let weak_five = five.downgrade();
+    /// ```
+    #[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+    #[unstable = "Weak pointers may not belong in this module"]
+    pub fn downgrade(&self) -> Weak<T> {
+        self.inc_weak();
+        Weak { _ptr: self._ptr }
+    }
 }
 
 /// Get the number of weak references to this value.
@@ -432,10 +497,31 @@ impl<T> Clone for Rc<T> {
     /// five.clone();
     /// ```
     #[inline]
+    #[cfg(stage0)] // NOTE remove after next snapshot
     fn clone(&self) -> Rc<T> {
         self.inc_strong();
         Rc { _ptr: self._ptr, _nosend: marker::NoSend, _noshare: marker::NoSync }
     }
+
+    /// Makes a clone of the `Rc<T>`.
+    ///
+    /// This increases the strong reference count.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::rc::Rc;
+    ///
+    /// let five = Rc::new(5i);
+    ///
+    /// five.clone();
+    /// ```
+    #[inline]
+    #[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+    fn clone(&self) -> Rc<T> {
+        self.inc_strong();
+        Rc { _ptr: self._ptr }
+    }
 }
 
 #[stable]
@@ -636,6 +722,7 @@ impl<T: fmt::String> fmt::String for Rc<T> {
 /// See the [module level documentation](../index.html) for more.
 #[unsafe_no_drop_flag]
 #[unstable = "Weak pointers may not belong in this module."]
+#[cfg(stage0)] // NOTE remove impl after next snapshot
 pub struct Weak<T> {
     // FIXME #12808: strange names to try to avoid interfering with
     // field accesses of the contained type via Deref
@@ -644,6 +731,29 @@ pub struct Weak<T> {
     _noshare: marker::NoSync
 }
 
+/// A weak version of `Rc<T>`.
+///
+/// Weak references do not count when determining if the inner value should be dropped.
+///
+/// See the [module level documentation](../index.html) for more.
+#[unsafe_no_drop_flag]
+#[unstable = "Weak pointers may not belong in this module."]
+#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+pub struct Weak<T> {
+    // FIXME #12808: strange names to try to avoid interfering with
+    // field accesses of the contained type via Deref
+    _ptr: NonZero<*mut RcBox<T>>,
+}
+
+#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+#[allow(unstable)]
+impl<T> !marker::Send for Weak<T> {}
+
+#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+#[allow(unstable)]
+impl<T> !marker::Sync for Weak<T> {}
+
+
 #[unstable = "Weak pointers may not belong in this module."]
 impl<T> Weak<T> {
     /// Upgrades a weak reference to a strong reference.
@@ -663,6 +773,7 @@ impl<T> Weak<T> {
     ///
     /// let strong_five: Option<Rc<_>> = weak_five.upgrade();
     /// ```
+    #[cfg(stage0)] // NOTE remove after next snapshot
     pub fn upgrade(&self) -> Option<Rc<T>> {
         if self.strong() == 0 {
             None
@@ -671,6 +782,33 @@ impl<T> Weak<T> {
             Some(Rc { _ptr: self._ptr, _nosend: marker::NoSend, _noshare: marker::NoSync })
         }
     }
+
+    /// Upgrades a weak reference to a strong reference.
+    ///
+    /// Upgrades the `Weak<T>` reference to an `Rc<T>`, if possible.
+    ///
+    /// Returns `None` if there were no strong references and the data was destroyed.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::rc::Rc;
+    ///
+    /// let five = Rc::new(5i);
+    ///
+    /// let weak_five = five.downgrade();
+    ///
+    /// let strong_five: Option<Rc<_>> = weak_five.upgrade();
+    /// ```
+    #[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+    pub fn upgrade(&self) -> Option<Rc<T>> {
+        if self.strong() == 0 {
+            None
+        } else {
+            self.inc_strong();
+            Some(Rc { _ptr: self._ptr })
+        }
+    }
 }
 
 #[unsafe_destructor]
@@ -733,10 +871,31 @@ impl<T> Clone for Weak<T> {
     /// weak_five.clone();
     /// ```
     #[inline]
+    #[cfg(stage0)] // NOTE remove after next snapshot
     fn clone(&self) -> Weak<T> {
         self.inc_weak();
         Weak { _ptr: self._ptr, _nosend: marker::NoSend, _noshare: marker::NoSync }
     }
+
+    /// Makes a clone of the `Weak<T>`.
+    ///
+    /// This increases the weak reference count.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::rc::Rc;
+    ///
+    /// let weak_five = Rc::new(5i).downgrade();
+    ///
+    /// weak_five.clone();
+    /// ```
+    #[inline]
+    #[cfg(not(stage0))] // NOTE remove cfg after next snapshot
+    fn clone(&self) -> Weak<T> {
+        self.inc_weak();
+        Weak { _ptr: self._ptr }
+    }
 }
 
 #[unstable = "Show is experimental."]