Skip to content

rc: Use ~T for allocation #8022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 43 additions & 47 deletions src/libextra/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ cycle cannot be created with `Rc<T>` because there is no way to modify it after


use std::cast;
use std::libc::{c_void, size_t, malloc, free};
use std::ptr;
use std::sys;
use std::unstable::intrinsics;

// Convert ~T into *mut T without dropping it
#[inline]
unsafe fn owned_to_raw<T>(mut box: ~T) -> *mut T {
let ptr = ptr::to_mut_unsafe_ptr(box);
intrinsics::forget(box);
ptr
}

struct RcBox<T> {
value: T,
count: uint
Expand All @@ -42,21 +48,20 @@ pub struct Rc<T> {

impl<T> Rc<T> {
unsafe fn new(value: T) -> Rc<T> {
let ptr = malloc(sys::size_of::<RcBox<T>>() as size_t) as *mut RcBox<T>;
assert!(!ptr::is_null(ptr));
intrinsics::move_val_init(&mut *ptr, RcBox{value: value, count: 1});
Rc{ptr: ptr}
Rc{ptr: owned_to_raw(~RcBox{value: value, count: 1})}
}
}

// FIXME: #6516: should be a static method
pub fn rc_from_owned<T: Send>(value: T) -> Rc<T> {
unsafe { Rc::new(value) }
impl<T: Send> Rc<T> {
pub fn from_owned(value: T) -> Rc<T> {
unsafe { Rc::new(value) }
}
}

// FIXME: #6516: should be a static method
pub fn rc_from_const<T: Freeze>(value: T) -> Rc<T> {
unsafe { Rc::new(value) }
impl<T: Freeze> Rc<T> {
pub fn from_const(value: T) -> Rc<T> {
unsafe { Rc::new(value) }
}
}

impl<T> Rc<T> {
Expand All @@ -73,8 +78,7 @@ impl<T> Drop for Rc<T> {
if self.ptr.is_not_null() {
(*self.ptr).count -= 1;
if (*self.ptr).count == 0 {
ptr::read_ptr(self.ptr);
free(self.ptr as *c_void)
let _: ~T = cast::transmute(self.ptr);
}
}
}
Expand Down Expand Up @@ -107,7 +111,7 @@ mod test_rc {

#[test]
fn test_clone() {
let x = rc_from_owned(Cell::new(5));
let x = Rc::from_owned(Cell::new(5));
let y = x.clone();
do x.borrow().with_mut_ref |inner| {
*inner = 20;
Expand All @@ -117,7 +121,7 @@ mod test_rc {

#[test]
fn test_deep_clone() {
let x = rc_from_owned(Cell::new(5));
let x = Rc::from_owned(Cell::new(5));
let y = x.deep_clone();
do x.borrow().with_mut_ref |inner| {
*inner = 20;
Expand All @@ -127,31 +131,25 @@ mod test_rc {

#[test]
fn test_simple() {
let x = rc_from_const(5);
let x = Rc::from_const(5);
assert_eq!(*x.borrow(), 5);
}

#[test]
fn test_simple_clone() {
let x = rc_from_const(5);
let x = Rc::from_const(5);
let y = x.clone();
assert_eq!(*x.borrow(), 5);
assert_eq!(*y.borrow(), 5);
}

#[test]
fn test_destructor() {
let x = rc_from_owned(~5);
let x = Rc::from_owned(~5);
assert_eq!(**x.borrow(), 5);
}
}

#[abi = "rust-intrinsic"]
extern "rust-intrinsic" {
fn init<T>() -> T;
fn uninit<T>() -> T;
}

#[deriving(Eq)]
enum Borrow {
Mutable,
Expand All @@ -175,21 +173,20 @@ pub struct RcMut<T> {

impl<T> RcMut<T> {
unsafe fn new(value: T) -> RcMut<T> {
let ptr = malloc(sys::size_of::<RcMutBox<T>>() as size_t) as *mut RcMutBox<T>;
assert!(!ptr::is_null(ptr));
intrinsics::move_val_init(&mut *ptr, RcMutBox{value: value, count: 1, borrow: Nothing});
RcMut{ptr: ptr}
RcMut{ptr: owned_to_raw(~RcMutBox{value: value, count: 1, borrow: Nothing})}
}
}

// FIXME: #6516: should be a static method
pub fn rc_mut_from_owned<T: Send>(value: T) -> RcMut<T> {
unsafe { RcMut::new(value) }
impl<T: Send> RcMut<T> {
pub fn from_owned(value: T) -> RcMut<T> {
unsafe { RcMut::new(value) }
}
}

// FIXME: #6516: should be a static method
pub fn rc_mut_from_const<T: Freeze>(value: T) -> RcMut<T> {
unsafe { RcMut::new(value) }
impl<T: Freeze> RcMut<T> {
pub fn from_const(value: T) -> RcMut<T> {
unsafe { RcMut::new(value) }
}
}

impl<T> RcMut<T> {
Expand Down Expand Up @@ -226,8 +223,7 @@ impl<T> Drop for RcMut<T> {
if self.ptr.is_not_null() {
(*self.ptr).count -= 1;
if (*self.ptr).count == 0 {
ptr::replace_ptr(self.ptr, uninit());
free(self.ptr as *c_void)
let _: ~T = cast::transmute(self.ptr);
}
}
}
Expand Down Expand Up @@ -262,7 +258,7 @@ mod test_rc_mut {

#[test]
fn test_clone() {
let x = rc_mut_from_owned(5);
let x = RcMut::from_owned(5);
let y = x.clone();
do x.with_mut_borrow |value| {
*value = 20;
Expand All @@ -274,7 +270,7 @@ mod test_rc_mut {

#[test]
fn test_deep_clone() {
let x = rc_mut_from_const(5);
let x = RcMut::from_const(5);
let y = x.deep_clone();
do x.with_mut_borrow |value| {
*value = 20;
Expand All @@ -286,7 +282,7 @@ mod test_rc_mut {

#[test]
fn borrow_many() {
let x = rc_mut_from_owned(5);
let x = RcMut::from_owned(5);
let y = x.clone();

do x.with_borrow |a| {
Expand All @@ -302,7 +298,7 @@ mod test_rc_mut {

#[test]
fn modify() {
let x = rc_mut_from_const(5);
let x = RcMut::from_const(5);
let y = x.clone();

do y.with_mut_borrow |a| {
Expand All @@ -317,22 +313,22 @@ mod test_rc_mut {

#[test]
fn release_immutable() {
let x = rc_mut_from_owned(5);
let x = RcMut::from_owned(5);
do x.with_borrow |_| {}
do x.with_mut_borrow |_| {}
}

#[test]
fn release_mutable() {
let x = rc_mut_from_const(5);
let x = RcMut::from_const(5);
do x.with_mut_borrow |_| {}
do x.with_borrow |_| {}
}

#[test]
#[should_fail]
fn frozen() {
let x = rc_mut_from_owned(5);
let x = RcMut::from_owned(5);
let y = x.clone();

do x.with_borrow |_| {
Expand All @@ -344,7 +340,7 @@ mod test_rc_mut {
#[test]
#[should_fail]
fn mutable_dupe() {
let x = rc_mut_from_const(5);
let x = RcMut::from_const(5);
let y = x.clone();

do x.with_mut_borrow |_| {
Expand All @@ -356,7 +352,7 @@ mod test_rc_mut {
#[test]
#[should_fail]
fn mutable_freeze() {
let x = rc_mut_from_owned(5);
let x = RcMut::from_owned(5);
let y = x.clone();

do x.with_mut_borrow |_| {
Expand All @@ -368,7 +364,7 @@ mod test_rc_mut {
#[test]
#[should_fail]
fn restore_freeze() {
let x = rc_mut_from_const(5);
let x = RcMut::from_const(5);
let y = x.clone();

do x.with_borrow |_| {
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/rcmut-not-const-and-not-owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn o<T: Send>(_: &T) {}
fn c<T: Freeze>(_: &T) {}

fn main() {
let x = extra::rc::rc_mut_from_owned(0);
let x = extra::rc::RcMut::from_owned(0);
o(&x); //~ ERROR instantiating a type parameter with an incompatible type `extra::rc::RcMut<int>`, which does not fulfill `Send`
c(&x); //~ ERROR instantiating a type parameter with an incompatible type `extra::rc::RcMut<int>`, which does not fulfill `Freeze`
}