From 8662141c21720e9398dd3230d6503c41db9de59e Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 6 Nov 2013 17:56:22 -0500 Subject: [PATCH] add `from_send` to Rc, since #9509 is fixed --- src/libstd/rc.rs | 45 ++++++++++++++------------- src/test/compile-fail/no_freeze-rc.rs | 19 +++++++++++ 2 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 src/test/compile-fail/no_freeze-rc.rs diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs index 41e834cf37c..cc2f739ce98 100644 --- a/src/libstd/rc.rs +++ b/src/libstd/rc.rs @@ -48,12 +48,21 @@ impl Rc { } } +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 { /// Unsafety construct a new reference-counted box from any value. /// - /// If the type is not `Freeze`, the `Rc` box will incorrectly still be considered as a `Freeze` - /// type. It is also possible to create cycles, which will leak, and may interact poorly with - /// managed pointers. + /// 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})} @@ -104,26 +113,22 @@ mod test_rc { #[test] fn test_clone() { - unsafe { - let x = Rc::new_unchecked(Cell::new(5)); - let y = x.clone(); - do x.borrow().with_mut_ref |inner| { - *inner = 20; - } - assert_eq!(y.borrow().take(), 20); + let x = Rc::from_send(Cell::new(5)); + let y = x.clone(); + do x.borrow().with_mut_ref |inner| { + *inner = 20; } + assert_eq!(y.borrow().take(), 20); } #[test] fn test_deep_clone() { - unsafe { - let x = Rc::new_unchecked(Cell::new(5)); - let y = x.deep_clone(); - do x.borrow().with_mut_ref |inner| { - *inner = 20; - } - assert_eq!(y.borrow().take(), 5); + let x = Rc::from_send(Cell::new(5)); + let y = x.deep_clone(); + do x.borrow().with_mut_ref |inner| { + *inner = 20; } + assert_eq!(y.borrow().take(), 5); } #[test] @@ -142,10 +147,8 @@ mod test_rc { #[test] fn test_destructor() { - unsafe { - let x = Rc::new_unchecked(~5); - assert_eq!(**x.borrow(), 5); - } + let x = Rc::from_send(~5); + assert_eq!(**x.borrow(), 5); } } diff --git a/src/test/compile-fail/no_freeze-rc.rs b/src/test/compile-fail/no_freeze-rc.rs new file mode 100644 index 00000000000..6185ccf2e7e --- /dev/null +++ b/src/test/compile-fail/no_freeze-rc.rs @@ -0,0 +1,19 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::rc::Rc; +use std::cell::Cell; + +fn bar(_: T) {} + +fn main() { + let x = Rc::from_send(Cell::new(5)); + bar(x); //~ ERROR instantiating a type parameter with an incompatible type `std::rc::Rc>`, which does not fulfill `Freeze` +}