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;
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> !marker::Sync for Rc<T> {}
-#[cfg(not(stage0))] // remove cfg after new snapshot
+// remove cfg after new snapshot
+#[cfg(not(stage0))]
#[unstable(feature = "coerce_unsized", issue = "27732")]
-impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T> {}
+impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T> {}
impl<T> Rc<T> {
/// Constructs a new `Rc<T>`.
}
impl<T: Clone> Rc<T> {
- #[inline]
- #[unstable(feature = "rc_make_unique", reason = "renamed to Rc::make_mut",
- issue = "27718")]
- #[rustc_deprecated(since = "1.4.0", reason = "renamed to Rc::make_mut")]
- pub fn make_unique(&mut self) -> &mut T {
- Rc::make_mut(self)
- }
-
/// Make a mutable reference into the given `Rc<T>` by cloning the inner
/// data if the `Rc<T>` doesn't have one strong reference and no weak
/// references.
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Clone for Rc<T> {
-
/// Makes a clone of the `Rc<T>`.
///
/// When you clone an `Rc<T>`, it will create another pointer to the data and
}
#[stable(feature = "rust1", since = "1.0.0")]
-impl<T: ?Sized+Hash> Hash for Rc<T> {
+impl<T: ?Sized + Hash> Hash for Rc<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}
#[stable(feature = "rust1", since = "1.0.0")]
-impl<T: ?Sized+fmt::Display> fmt::Display for Rc<T> {
+impl<T: ?Sized + fmt::Display> fmt::Display for Rc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
-impl<T: ?Sized+fmt::Debug> fmt::Debug for Rc<T> {
+impl<T: ?Sized + fmt::Debug> fmt::Debug for Rc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> !marker::Sync for Weak<T> {}
-#[cfg(not(stage0))] // remove cfg after new snapshot
+// remove cfg after new snapshot
+#[cfg(not(stage0))]
#[unstable(feature = "coerce_unsized", issue = "27732")]
-impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
+impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
impl<T: ?Sized> Weak<T> {
/// Upgrades a weak reference to a strong reference.
#[stable(feature = "rc_weak", since = "1.4.0")]
impl<T: ?Sized> Clone for Weak<T> {
-
/// Makes a clone of the `Weak<T>`.
///
/// This increases the weak reference count.
}
#[stable(feature = "rust1", since = "1.0.0")]
-impl<T: ?Sized+fmt::Debug> fmt::Debug for Weak<T> {
+impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(Weak)")
}
}
+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.
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")]