Skip to content

Commit b36a948

Browse files
committed
stop treating Rc cycles as unsafe
1 parent 63ba93f commit b36a948

File tree

5 files changed

+13
-70
lines changed

5 files changed

+13
-70
lines changed

src/libextra/serialize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,14 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for @T {
406406
}
407407
}
408408

409-
impl<S:Encoder,T:Encodable<S> + Freeze> Encodable<S> for Rc<T> {
409+
impl<S:Encoder,T:Encodable<S>> Encodable<S> for Rc<T> {
410410
#[inline]
411411
fn encode(&self, s: &mut S) {
412412
self.borrow().encode(s)
413413
}
414414
}
415415

416-
impl<D:Decoder,T:Decodable<D> + Freeze> Decodable<D> for Rc<T> {
416+
impl<D:Decoder,T:Decodable<D> + NonManaged> Decodable<D> for Rc<T> {
417417
#[inline]
418418
fn decode(d: &mut D) -> Rc<T> {
419419
Rc::new(Decodable::decode(d))

src/libstd/rc.rs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ overhead of atomic reference counting.
1919
use ptr::RawPtr;
2020
use unstable::intrinsics::transmute;
2121
use ops::Drop;
22-
use kinds::{Freeze, Send};
22+
use kinds::NonManaged;
2323
use clone::{Clone, DeepClone};
24-
use cell::RefCell;
2524
use cmp::{Eq, TotalEq, Ord, TotalOrd, Ordering};
2625

2726
struct RcBox<T> {
@@ -36,46 +35,17 @@ pub struct Rc<T> {
3635
priv ptr: *mut RcBox<T>
3736
}
3837

39-
impl<T: Freeze> Rc<T> {
40-
/// Construct a new reference-counted box from a `Freeze` value
38+
impl<T: NonManaged> Rc<T> {
39+
/// Construct a new reference-counted box
4140
#[inline]
4241
pub fn new(value: T) -> Rc<T> {
4342
unsafe {
44-
Rc::new_unchecked(value)
45-
}
46-
}
47-
}
48-
49-
impl<T: Send> Rc<T> {
50-
/// Construct a new reference-counted box from a `Send` value
51-
#[inline]
52-
pub fn from_send(value: T) -> Rc<T> {
53-
unsafe {
54-
Rc::new_unchecked(value)
55-
}
56-
}
57-
}
58-
59-
impl<T: Freeze> Rc<RefCell<T>> {
60-
/// Construct a new reference-counted box from a `RefCell`-wrapped `Freeze` value
61-
#[inline]
62-
pub fn from_mut(value: RefCell<T>) -> Rc<RefCell<T>> {
63-
unsafe {
64-
Rc::new_unchecked(value)
43+
Rc { ptr: transmute(~RcBox { value: value, count: 1 }) }
6544
}
6645
}
6746
}
6847

6948
impl<T> Rc<T> {
70-
/// Unsafety construct a new reference-counted box from any value.
71-
///
72-
/// It is possible to create cycles, which will leak, and may interact
73-
/// poorly with managed pointers.
74-
#[inline]
75-
pub unsafe fn new_unchecked(value: T) -> Rc<T> {
76-
Rc{ptr: transmute(~RcBox{value: value, count: 1})}
77-
}
78-
7949
/// Borrow the value contained in the reference-counted box
8050
#[inline]
8151
pub fn borrow<'r>(&'r self) -> &'r T {
@@ -147,10 +117,10 @@ impl<T> Clone for Rc<T> {
147117
}
148118
}
149119

150-
impl<T: DeepClone> DeepClone for Rc<T> {
120+
impl<T: NonManaged + DeepClone> DeepClone for Rc<T> {
151121
#[inline]
152122
fn deep_clone(&self) -> Rc<T> {
153-
unsafe { Rc::new_unchecked(self.borrow().deep_clone()) }
123+
Rc::new(self.borrow().deep_clone())
154124
}
155125
}
156126

@@ -176,7 +146,7 @@ mod test_rc {
176146

177147
#[test]
178148
fn test_clone() {
179-
let x = Rc::from_send(RefCell::new(5));
149+
let x = Rc::new(RefCell::new(5));
180150
let y = x.clone();
181151
x.borrow().with_mut(|inner| {
182152
*inner = 20;
@@ -186,7 +156,7 @@ mod test_rc {
186156

187157
#[test]
188158
fn test_deep_clone() {
189-
let x = Rc::from_send(RefCell::new(5));
159+
let x = Rc::new(RefCell::new(5));
190160
let y = x.deep_clone();
191161
x.borrow().with_mut(|inner| {
192162
*inner = 20;
@@ -210,13 +180,7 @@ mod test_rc {
210180

211181
#[test]
212182
fn test_destructor() {
213-
let x = Rc::from_send(~5);
183+
let x = Rc::new(~5);
214184
assert_eq!(**x.borrow(), 5);
215185
}
216-
217-
#[test]
218-
fn test_from_mut() {
219-
let a = 10;
220-
let _x = Rc::from_mut(RefCell::new(&a));
221-
}
222186
}

src/test/compile-fail/issue-7013.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct A
3737
fn main()
3838
{
3939
let a = A {v: ~B{v: None} as ~Foo}; //~ ERROR cannot pack type `~B`, which does not fulfill `Send`
40-
let v = Rc::from_send(RefCell::new(a));
40+
let v = Rc::new(RefCell::new(a));
4141
let w = v.clone();
4242
let b = v.borrow();
4343
let mut b = b.borrow_mut();

src/test/compile-fail/no_freeze-rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ use std::cell::RefCell;
1414
fn bar<T: Freeze>(_: T) {}
1515

1616
fn main() {
17-
let x = Rc::from_send(RefCell::new(5));
17+
let x = Rc::new(RefCell::new(5));
1818
bar(x); //~ ERROR instantiating a type parameter with an incompatible type `std::rc::Rc<std::cell::RefCell<int>>`, which does not fulfill `Freeze`
1919
}

src/test/compile-fail/rcmut-not-const-and-not-owned.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)