mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 11:44:28 +00:00
auto merge of #10322 : thestinger/rust/no_freeze, r=alexcrichton
This commit is contained in:
commit
a5f6f853f1
@ -48,12 +48,21 @@ impl<T: Freeze> Rc<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Send> Rc<T> {
|
||||||
|
/// Construct a new reference-counted box from a `Send` value
|
||||||
|
#[inline]
|
||||||
|
pub fn from_send(value: T) -> Rc<T> {
|
||||||
|
unsafe {
|
||||||
|
Rc::new_unchecked(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> Rc<T> {
|
impl<T> Rc<T> {
|
||||||
/// Unsafety construct a new reference-counted box from any value.
|
/// 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`
|
/// It is possible to create cycles, which will leak, and may interact
|
||||||
/// type. It is also possible to create cycles, which will leak, and may interact poorly with
|
/// poorly with managed pointers.
|
||||||
/// managed pointers.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn new_unchecked(value: T) -> Rc<T> {
|
pub unsafe fn new_unchecked(value: T) -> Rc<T> {
|
||||||
Rc{ptr: transmute(~RcBox{value: value, count: 1})}
|
Rc{ptr: transmute(~RcBox{value: value, count: 1})}
|
||||||
@ -104,26 +113,22 @@ mod test_rc {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_clone() {
|
fn test_clone() {
|
||||||
unsafe {
|
let x = Rc::from_send(Cell::new(5));
|
||||||
let x = Rc::new_unchecked(Cell::new(5));
|
let y = x.clone();
|
||||||
let y = x.clone();
|
do x.borrow().with_mut_ref |inner| {
|
||||||
do x.borrow().with_mut_ref |inner| {
|
*inner = 20;
|
||||||
*inner = 20;
|
|
||||||
}
|
|
||||||
assert_eq!(y.borrow().take(), 20);
|
|
||||||
}
|
}
|
||||||
|
assert_eq!(y.borrow().take(), 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deep_clone() {
|
fn test_deep_clone() {
|
||||||
unsafe {
|
let x = Rc::from_send(Cell::new(5));
|
||||||
let x = Rc::new_unchecked(Cell::new(5));
|
let y = x.deep_clone();
|
||||||
let y = x.deep_clone();
|
do x.borrow().with_mut_ref |inner| {
|
||||||
do x.borrow().with_mut_ref |inner| {
|
*inner = 20;
|
||||||
*inner = 20;
|
|
||||||
}
|
|
||||||
assert_eq!(y.borrow().take(), 5);
|
|
||||||
}
|
}
|
||||||
|
assert_eq!(y.borrow().take(), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -142,10 +147,8 @@ mod test_rc {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_destructor() {
|
fn test_destructor() {
|
||||||
unsafe {
|
let x = Rc::from_send(~5);
|
||||||
let x = Rc::new_unchecked(~5);
|
assert_eq!(**x.borrow(), 5);
|
||||||
assert_eq!(**x.borrow(), 5);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
src/test/compile-fail/no_freeze-rc.rs
Normal file
19
src/test/compile-fail/no_freeze-rc.rs
Normal file
@ -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 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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: Freeze>(_: 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<std::cell::Cell<int>>`, which does not fulfill `Freeze`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user