From b36a948831f7375b725f26ebec1df88687fdfeab Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 11 Dec 2013 18:03:25 -0500 Subject: stop treating `Rc` cycles as unsafe --- src/libstd/rc.rs | 54 +++++++++--------------------------------------------- 1 file changed, 9 insertions(+), 45 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs index ad2305c2410..9e622dce4c0 100644 --- a/src/libstd/rc.rs +++ b/src/libstd/rc.rs @@ -19,9 +19,8 @@ overhead of atomic reference counting. use ptr::RawPtr; use unstable::intrinsics::transmute; use ops::Drop; -use kinds::{Freeze, Send}; +use kinds::NonManaged; use clone::{Clone, DeepClone}; -use cell::RefCell; use cmp::{Eq, TotalEq, Ord, TotalOrd, Ordering}; struct RcBox { @@ -36,46 +35,17 @@ pub struct Rc { priv ptr: *mut RcBox } -impl Rc { - /// Construct a new reference-counted box from a `Freeze` value +impl Rc { + /// Construct a new reference-counted box #[inline] pub fn new(value: T) -> Rc { unsafe { - Rc::new_unchecked(value) - } - } -} - -impl Rc { - /// Construct a new reference-counted box from a `Send` value - #[inline] - pub fn from_send(value: T) -> Rc { - unsafe { - Rc::new_unchecked(value) - } - } -} - -impl Rc> { - /// Construct a new reference-counted box from a `RefCell`-wrapped `Freeze` value - #[inline] - pub fn from_mut(value: RefCell) -> Rc> { - unsafe { - Rc::new_unchecked(value) + Rc { ptr: transmute(~RcBox { value: value, count: 1 }) } } } } impl Rc { - /// Unsafety construct a new reference-counted box from any value. - /// - /// It is possible to create cycles, which will leak, and may interact - /// poorly with managed pointers. - #[inline] - pub unsafe fn new_unchecked(value: T) -> Rc { - Rc{ptr: transmute(~RcBox{value: value, count: 1})} - } - /// Borrow the value contained in the reference-counted box #[inline] pub fn borrow<'r>(&'r self) -> &'r T { @@ -147,10 +117,10 @@ impl Clone for Rc { } } -impl DeepClone for Rc { +impl DeepClone for Rc { #[inline] fn deep_clone(&self) -> Rc { - unsafe { Rc::new_unchecked(self.borrow().deep_clone()) } + Rc::new(self.borrow().deep_clone()) } } @@ -176,7 +146,7 @@ mod test_rc { #[test] fn test_clone() { - let x = Rc::from_send(RefCell::new(5)); + let x = Rc::new(RefCell::new(5)); let y = x.clone(); x.borrow().with_mut(|inner| { *inner = 20; @@ -186,7 +156,7 @@ mod test_rc { #[test] fn test_deep_clone() { - let x = Rc::from_send(RefCell::new(5)); + let x = Rc::new(RefCell::new(5)); let y = x.deep_clone(); x.borrow().with_mut(|inner| { *inner = 20; @@ -210,13 +180,7 @@ mod test_rc { #[test] fn test_destructor() { - let x = Rc::from_send(~5); + let x = Rc::new(~5); assert_eq!(**x.borrow(), 5); } - - #[test] - fn test_from_mut() { - let a = 10; - let _x = Rc::from_mut(RefCell::new(&a)); - } } -- cgit 1.4.1-3-g733a5