test: De-`@mut` the test suite

This commit is contained in:
Patrick Walton 2013-12-31 15:46:27 -08:00
parent df13c64c3b
commit c3694d732e
72 changed files with 357 additions and 843 deletions

View File

@ -10,6 +10,8 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
pub struct Entry<A,B> {
key: A,
value: B
@ -17,11 +19,12 @@ pub struct Entry<A,B> {
pub struct alist<A,B> {
eq_fn: extern "Rust" fn(A,A) -> bool,
data: @mut ~[Entry<A,B>]
data: @RefCell<~[Entry<A,B>]>,
}
pub fn alist_add<A:'static,B:'static>(lst: &alist<A,B>, k: A, v: B) {
lst.data.push(Entry{key:k, value:v});
let mut data = lst.data.borrow_mut();
data.get().push(Entry{key:k, value:v});
}
pub fn alist_get<A:Clone + 'static,
@ -30,7 +33,8 @@ pub fn alist_get<A:Clone + 'static,
k: A)
-> B {
let eq_fn = lst.eq_fn;
for entry in lst.data.iter() {
let data = lst.data.borrow();
for entry in data.get().iter() {
if eq_fn(entry.key.clone(), k.clone()) {
return entry.value.clone();
}
@ -41,12 +45,18 @@ pub fn alist_get<A:Clone + 'static,
#[inline]
pub fn new_int_alist<B:'static>() -> alist<int, B> {
fn eq_int(a: int, b: int) -> bool { a == b }
return alist {eq_fn: eq_int, data: @mut ~[]};
return alist {
eq_fn: eq_int,
data: @RefCell::new(~[]),
};
}
#[inline]
pub fn new_int_alist_2<B:'static>() -> alist<int, B> {
#[inline]
fn eq_int(a: int, b: int) -> bool { a == b }
return alist {eq_fn: eq_int, data: @mut ~[]};
return alist {
eq_fn: eq_int,
data: @RefCell::new(~[]),
};
}

View File

@ -14,11 +14,12 @@
extern mod extra;
use std::cell::RefCell;
use std::hashmap::HashMap;
pub type header_map = HashMap<~str, @mut ~[@~str]>;
pub type header_map = HashMap<~str, @RefCell<~[@~str]>>;
// the unused ty param is necessary so this gets monomorphized
pub fn request<T>(req: &header_map) {
let _x = (*((**req.get(&~"METHOD")).clone())[0u]).clone();
let _x = (*((**req.get(&~"METHOD")).clone()).get()[0u]).clone();
}

View File

@ -86,7 +86,7 @@ impl Sudoku {
return Sudoku::new(g)
}
pub fn write(&self, writer: @mut io::Writer) {
pub fn write(&self, writer: &mut io::Writer) {
for row in range(0u8, 9u8) {
write!(writer, "{}", self.grid[row][0]);
for col in range(1u8, 9u8) {
@ -280,5 +280,5 @@ fn main() {
Sudoku::read(BufferedReader::new(io::stdin()))
};
sudoku.solve();
sudoku.write(@mut io::stdout() as @mut io::Writer);
sudoku.write(&mut io::stdout());
}

View File

@ -1,30 +0,0 @@
// 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.
#[feature(managed_boxes)];
trait T {
fn foo(@mut self);
}
struct S {
unused: int
}
impl T for S {
fn foo(@mut self) {
}
}
fn main() {
let s = @S { unused: 0 };
let _s2 = s as @mut T; //~ error: types differ in mutability
let _s3 = &s as &mut T; //~ error: types differ in mutability
}

View File

@ -8,19 +8,18 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[feature(managed_boxes)];
fn takes_imm(x: &int) { }
fn takes_mut(x: @mut int) { }
fn takes_imm(x: @int) { }
fn takes_mut(x: &mut int) { }
fn apply<T>(t: T, f: |T|) {
f(t)
}
fn main() {
apply(@3, takes_mut); //~ ERROR (values differ in mutability)
apply(@3, takes_imm);
apply(&3, takes_mut); //~ ERROR (values differ in mutability)
apply(&3, takes_imm);
apply(@mut 3, takes_mut);
apply(@mut 3, takes_imm); //~ ERROR (values differ in mutability)
apply(&mut 3, takes_mut);
apply(&mut 3, takes_imm); //~ ERROR (values differ in mutability)
}

View File

@ -12,6 +12,5 @@
static x: ~[int] = ~[123, 456]; //~ ERROR: cannot allocate vectors in constant expressions
static y: @[int] = @[123, 456]; //~ ERROR: cannot allocate vectors in constant expressions
static z: @mut [int] = @mut [123, 456]; //~ ERROR: cannot allocate vectors in constant expressions
fn main() {}

View File

@ -12,10 +12,10 @@
// A test case for #2548.
use std::cell::Cell;
struct foo {
x: @mut int,
x: @Cell<int>,
}
#[unsafe_destructor]
@ -23,17 +23,17 @@ impl Drop for foo {
fn drop(&mut self) {
unsafe {
println("Goodbye, World!");
*self.x += 1;
self.x.set(self.x.get() + 1);
}
}
}
fn foo(x: @mut int) -> foo {
fn foo(x: @Cell<int>) -> foo {
foo { x: x }
}
fn main() {
let x = @mut 0;
let x = @Cell::new(0);
{
let mut res = foo(x);
@ -43,5 +43,5 @@ fn main() {
assert_eq!(v.len(), 2);
}
assert_eq!(*x, 1);
assert_eq!(x.get(), 1);
}

View File

@ -10,7 +10,7 @@
#[feature(managed_boxes)];
struct P { child: Option<@mut P> }
struct P { child: Option<@P> }
trait PTrait {
fn getChildOption(&self) -> Option<@P>;
}

View File

@ -1,12 +1,11 @@
#[feature(managed_boxes)];
struct Foo {
f: @mut int,
f: @int,
}
impl Drop for Foo { //~ ERROR cannot implement a destructor on a structure that does not satisfy Send
fn drop(&mut self) {
*self.f = 10;
}
}

View File

@ -1,26 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
fn two_args<T>(x: T, y: T) { }
fn main() {
let a: @mut int = @mut 3;
let b: @int = @3;
// NOTE:
//
// The fact that this test fails to compile reflects a known
// shortcoming of the current inference algorithm. These errors
// are *not* desirable.
two_args(a, b); //~ ERROR (values differ in mutability)
}

View File

@ -15,7 +15,6 @@ trait Foo {
fn borrowed_mut(&mut self);
fn managed(@self);
fn managed_mut(@mut self);
fn owned(~self);
}
@ -24,7 +23,6 @@ fn borrowed_receiver(x: &Foo) {
x.borrowed();
x.borrowed_mut(); // See [1]
x.managed(); //~ ERROR does not implement any method
x.managed_mut(); //~ ERROR does not implement any method
x.owned(); //~ ERROR does not implement any method
}
@ -32,7 +30,6 @@ fn borrowed_mut_receiver(x: &mut Foo) {
x.borrowed();
x.borrowed_mut();
x.managed(); //~ ERROR does not implement any method
x.managed_mut(); //~ ERROR does not implement any method
x.owned(); //~ ERROR does not implement any method
}
@ -40,15 +37,6 @@ fn managed_receiver(x: @Foo) {
x.borrowed();
x.borrowed_mut(); // See [1]
x.managed();
x.managed_mut(); //~ ERROR does not implement any method
x.owned(); //~ ERROR does not implement any method
}
fn managed_mut_receiver(x: @mut Foo) {
x.borrowed();
x.borrowed_mut();
x.managed(); //~ ERROR does not implement any method
x.managed_mut();
x.owned(); //~ ERROR does not implement any method
}
@ -56,7 +44,6 @@ fn owned_receiver(x: ~Foo) {
x.borrowed();
x.borrowed_mut(); // See [1]
x.managed(); //~ ERROR does not implement any method
x.managed_mut(); //~ ERROR does not implement any method
x.owned();
}

View File

@ -10,20 +10,22 @@
#[feature(managed_boxes)];
use std::cell::Cell;
struct r {
i: @mut int,
i: @Cell<int>,
}
#[unsafe_destructor]
impl Drop for r {
fn drop(&mut self) {
unsafe {
*(self.i) = *(self.i) + 1;
self.i.set(self.i.get() + 1);
}
}
}
fn r(i: @mut int) -> r {
fn r(i: @Cell<int>) -> r {
r {
i: i
}
@ -34,7 +36,7 @@ struct A {
}
fn main() {
let i = @mut 0;
let i = @Cell::new(0);
{
// Can't do this copy
let x = ~~~A {y: r(i)};

View File

@ -11,7 +11,7 @@
#[feature(managed_boxes)];
struct invariant<'a> {
f: 'static |x: @mut &'a int|
f: 'static |x: &mut &'a int|
}
fn to_same_lifetime<'r>(bi: invariant<'r>) {

View File

@ -11,7 +11,7 @@
#[feature(managed_boxes)];
struct invariant<'a> {
f: 'static || -> @mut &'a int
f: 'static || -> &mut &'a int
}
fn to_same_lifetime<'r>(bi: invariant<'r>) {

View File

@ -10,15 +10,17 @@
#[feature(managed_boxes)];
use std::cell::Cell;
struct r {
i: @mut int,
i: @Cell<int>,
}
#[unsafe_destructor]
impl Drop for r {
fn drop(&mut self) {
unsafe {
*(self.i) = *(self.i) + 1;
self.i.set(self.i.get() + 1);
}
}
}
@ -27,12 +29,12 @@ fn f<T>(_i: ~[T], _j: ~[T]) {
}
fn main() {
let i1 = @mut 0;
let i2 = @mut 1;
let i1 = @Cell::new(0);
let i2 = @Cell::new(1);
let r1 = ~[~r { i: i1 }];
let r2 = ~[~r { i: i2 }];
f(r1.clone(), r2.clone());
//~^ ERROR failed to find an implementation of
info!("{:?}", (r2, *i1));
info!("{:?}", (r1, *i2));
info!("{:?}", (r2, i1.get()));
info!("{:?}", (r1, i2.get()));
}

View File

@ -14,6 +14,8 @@
#[feature(managed_boxes)];
use std::cell::Cell;
fn test1() { let val = @0; { } *val; }
fn test2() -> int { let val = @0; { } *val }
@ -21,9 +23,9 @@ fn test2() -> int { let val = @0; { } *val }
struct S { eax: int }
fn test3() {
let regs = @mut S {eax: 0};
let regs = @Cell::new(S {eax: 0});
match true { true => { } _ => { } }
(*regs).eax = 1;
regs.set(S {eax: 1});
}
fn test4() -> bool { let regs = @true; if true { } *regs || false }
@ -51,10 +53,13 @@ fn test8() -> int {
}
}
fn test9() { let regs = @mut 0; match true { true => { } _ => { } } *regs += 1; }
fn test9() {
let regs = @Cell::new(0);
match true { true => { } _ => { } } regs.set(regs.get() + 1);
}
fn test10() -> int {
let regs = @mut ~[0];
let regs = @~[0];
match true { true => { } _ => { } }
(*regs)[0]
}

View File

@ -14,7 +14,7 @@
#[feature(managed_boxes)];
fn main() {
let _count = @mut 0u;
let _count = @0u;
let mut map = std::hashmap::HashMap::new();
let mut arr = ~[];
for _i in range(0u, 10u) {

View File

@ -10,6 +10,8 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
struct Pair<A,B> {
a: A, b: B
}
@ -17,12 +19,15 @@ struct Pair<A,B> {
struct RecEnum<A>(Rec<A>);
struct Rec<A> {
val: A,
rec: Option<@mut RecEnum<A>>
rec: Option<@RefCell<RecEnum<A>>>
}
fn make_cycle<A:'static>(a: A) {
let g: @mut RecEnum<A> = @mut RecEnum(Rec {val: a, rec: None});
g.rec = Some(g);
let g: @RefCell<RecEnum<A>> = @RefCell::new(RecEnum(Rec {val: a, rec: None}));
{
let mut gb = g.borrow_mut();
gb.get().rec = Some(g);
}
}
struct Invoker<A,B> {

View File

@ -35,8 +35,8 @@ fn test_ebml<'a, A:
Encodable<EBWriter::Encoder> +
Decodable<EBReader::Decoder<'a>>
>(a1: &A) {
let mut wr = @mut std::io::mem::MemWriter::new();
let mut ebml_w = EBWriter::Encoder(wr);
let mut wr = std::io::mem::MemWriter::new();
let mut ebml_w = EBWriter::Encoder(&mut wr);
a1.encode(&mut ebml_w);
let bytes = wr.inner_ref().as_slice();

View File

@ -12,15 +12,18 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
pub fn main() {
let x: @mut @Option<~int> = @mut @None;
match x {
@@Some(ref _y) => {
let x: @RefCell<@Option<~int>> = @RefCell::new(@None);
let mut xb = x.borrow_mut();
match *xb.get() {
@Some(ref _y) => {
// here, the refcount of `*x` is bumped so
// `_y` remains valid even if `*x` is modified.
*x = @None;
*xb.get() = @None;
}
@@None => {
@None => {
// here, no bump of the ref count of `*x` is needed, but in
// fact a bump occurs anyway because of how pattern marching
// works.

View File

@ -12,23 +12,25 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
use std::ptr;
struct F { f: ~int }
pub fn main() {
let x = @mut @F {f: ~3};
match x {
@@F{f: ref b_x} => {
let x = @RefCell::new(@F {f: ~3});
let mut xb = x.borrow_mut();
match *xb.get() {
@F{f: ref b_x} => {
assert_eq!(**b_x, 3);
assert_eq!(ptr::to_unsafe_ptr(&(x.f)), ptr::to_unsafe_ptr(b_x));
assert_eq!(ptr::to_unsafe_ptr(&(xb.get().f)), ptr::to_unsafe_ptr(b_x));
*x = @F {f: ~4};
*xb.get() = @F {f: ~4};
info!("ptr::to_unsafe_ptr(*b_x) = {:x}",
ptr::to_unsafe_ptr(&(**b_x)) as uint);
assert_eq!(**b_x, 3);
assert!(ptr::to_unsafe_ptr(&(*x.f)) != ptr::to_unsafe_ptr(&(**b_x)));
assert!(ptr::to_unsafe_ptr(&(*xb.get().f)) != ptr::to_unsafe_ptr(&(**b_x)));
}
}
}

View File

@ -10,6 +10,8 @@
#[feature(managed_boxes)];
use std::cell::Cell;
enum newtype {
newtype(int)
}
@ -19,12 +21,12 @@ pub fn main() {
// Test that borrowck treats enums with a single variant
// specially.
let x = @mut 5;
let y = @mut newtype(3);
let z = match *y {
let x = @Cell::new(5);
let y = @Cell::new(newtype(3));
let z = match y.get() {
newtype(b) => {
*x += 1;
*x * b
x.set(x.get() + 1);
x.get() * b
}
};
assert_eq!(z, 18);

View File

@ -1,36 +0,0 @@
// 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.
#[feature(managed_boxes)];
trait T {
fn foo(@mut self);
}
struct S {
unused: int
}
impl T for S {
fn foo(@mut self) {
}
}
fn bar(t: @mut T) {
t.foo();
}
pub fn main() {
let s = @mut S { unused: 0 };
let s2 = s as @mut T;
s2.foo();
bar(s2);
bar(s as @mut T);
}

View File

@ -11,86 +11,92 @@
#[feature(managed_boxes)];
trait noisy {
fn speak(&self) -> int;
fn speak(&mut self) -> int;
}
struct dog {
priv barks : @mut uint,
priv barks: uint,
volume : @mut int,
volume: int,
}
impl dog {
fn bark(&self) -> int {
info!("Woof {} {}", *self.barks, *self.volume);
*self.barks += 1u;
if *self.barks % 3u == 0u {
*self.volume += 1;
fn bark(&mut self) -> int {
info!("Woof {} {}", self.barks, self.volume);
self.barks += 1u;
if self.barks % 3u == 0u {
self.volume += 1;
}
if *self.barks % 10u == 0u {
*self.volume -= 2;
if self.barks % 10u == 0u {
self.volume -= 2;
}
info!("Grrr {} {}", *self.barks, *self.volume);
*self.volume
info!("Grrr {} {}", self.barks, self.volume);
self.volume
}
}
impl noisy for dog {
fn speak(&self) -> int { self.bark() }
fn speak(&mut self) -> int {
self.bark()
}
}
fn dog() -> dog {
dog {
volume: @mut 0,
barks: @mut 0u
volume: 0,
barks: 0u
}
}
#[deriving(Clone)]
struct cat {
priv meows : @mut uint,
priv meows: uint,
how_hungry : @mut int,
name : ~str,
how_hungry: int,
name: ~str,
}
impl noisy for cat {
fn speak(&self) -> int { self.meow() as int }
}
impl cat {
pub fn meow_count(&self) -> uint { *self.meows }
}
impl cat {
fn meow(&self) -> uint {
info!("Meow");
*self.meows += 1u;
if *self.meows % 5u == 0u {
*self.how_hungry += 1;
}
*self.meows
fn speak(&mut self) -> int {
self.meow() as int
}
}
fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
impl cat {
pub fn meow_count(&self) -> uint {
self.meows
}
}
impl cat {
fn meow(&mut self) -> uint {
info!("Meow");
self.meows += 1u;
if self.meows % 5u == 0u {
self.how_hungry += 1;
}
self.meows
}
}
fn cat(in_x: uint, in_y: int, in_name: ~str) -> cat {
cat {
meows: @mut in_x,
how_hungry: @mut in_y,
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
fn annoy_neighbors(critter: @noisy) {
fn annoy_neighbors(critter: &mut noisy) {
for _i in range(0u, 10) { critter.speak(); }
}
pub fn main() {
let nyan : cat = cat(0u, 2, ~"nyan");
let whitefang : dog = dog();
annoy_neighbors(@nyan.clone() as @noisy);
annoy_neighbors(@whitefang as @noisy);
let mut nyan: cat = cat(0u, 2, ~"nyan");
let mut whitefang: dog = dog();
annoy_neighbors(&mut nyan);
annoy_neighbors(&mut whitefang);
assert_eq!(nyan.meow_count(), 10u);
assert_eq!(*whitefang.volume, 1);
assert_eq!(whitefang.volume, 1);
}

View File

@ -58,6 +58,7 @@ fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
pub fn main() {
let nyan: @mut noisy = @mut cat(0u, 2, ~"nyan") as @mut noisy;
nyan.speak();
let mut nyan = cat(0u, 2, ~"nyan");
let mut nyan: &mut noisy = &mut nyan;
nyan.speak();
}

View File

@ -10,14 +10,16 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
enum taggy {
cons(@mut taggy),
cons(@RefCell<taggy>),
nil,
}
fn f() {
let a_box = @mut nil;
*a_box = cons(a_box);
let a_box = @RefCell::new(nil);
a_box.set(cons(a_box));
}
pub fn main() {

View File

@ -1,42 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
struct foo {
z: Option<@Invokable>,
}
struct Thing {
w: @mut foo,
}
trait Invokable {
fn f(&self);
}
impl Invokable for Thing {
fn f(&self) {
nop_foo(self.w);
}
}
fn nop() { }
fn nop_foo(_x : @mut foo) { }
pub fn main() {
let w = @mut foo {
z: None,
};
let x = @Thing {
w: w,
} as @Invokable;
w.z = Some(x);
}

View File

@ -10,4 +10,10 @@
#[feature(managed_boxes)];
pub fn main() { let x = @mut 5; *x = 1000; info!("{:?}", *x); }
use std::cell::Cell;
pub fn main() {
let x = @Cell::new(5);
x.set(1000);
info!("{:?}", x.get());
}

View File

@ -58,8 +58,8 @@ struct G<T> {
fn roundtrip<'a, T: Rand + Eq + Encodable<Encoder> +
Decodable<Decoder<'a>>>() {
let obj: T = random();
let w = @mut MemWriter::new();
let mut e = Encoder(w);
let mut w = MemWriter::new();
let mut e = Encoder(&mut w);
obj.encode(&mut e);
let doc = ebml::reader::Doc(@w.inner_ref().to_owned());
let mut dec = Decoder(doc);

View File

@ -30,7 +30,7 @@ struct Lots {
e: char,
f: f64,
g: (f32, char),
h: @mut (int, int),
h: @(int, int),
i: bool,
j: (),
}

View File

@ -18,9 +18,6 @@ impl Box {
pub fn set_many(&mut self, xs: &[uint]) {
for x in xs.iter() { self.x = *x; }
}
pub fn set_many2(@mut self, xs: &[uint]) {
for x in xs.iter() { self.x = *x; }
}
}
pub fn main() {}

View File

@ -13,10 +13,4 @@ pub fn main() {
println(v[2].to_str());
println(v[3].to_str());
println(v[4].to_str());
let v: @mut [int] = @mut [ 3, ..5 ];
println((v[0]).to_str());
println((v[1]).to_str());
println((v[2]).to_str());
println((v[3]).to_str());
println((v[4]).to_str());
}

View File

@ -10,15 +10,21 @@
#[feature(managed_boxes)];
use std::cell::Cell;
struct Point {x: int, y: int, z: int}
fn f(p: @mut Point) { assert!((p.z == 12)); p.z = 13; assert!((p.z == 13)); }
fn f(p: @Cell<Point>) {
assert!((p.get().z == 12));
p.set(Point {x: 10, y: 11, z: 13});
assert!((p.get().z == 13));
}
pub fn main() {
let a: Point = Point {x: 10, y: 11, z: 12};
let b: @mut Point = @mut a;
assert_eq!(b.z, 12);
let b: @Cell<Point> = @Cell::new(a);
assert_eq!(b.get().z, 12);
f(b);
assert_eq!(a.z, 12);
assert_eq!(b.z, 13);
assert_eq!(b.get().z, 13);
}

View File

@ -40,7 +40,7 @@ mod map_reduce {
}
fn map_task(ctrl: SharedChan<ctrl_proto>, input: ~str) {
let intermediates = @mut HashMap::new();
let mut intermediates = HashMap::new();
fn emit(im: &mut HashMap<~str, int>,
ctrl: SharedChan<ctrl_proto>, key: ~str,
@ -58,7 +58,7 @@ mod map_reduce {
}
let ctrl_clone = ctrl.clone();
::map(input, |a,b| emit(intermediates, ctrl.clone(), a, b) );
::map(input, |a,b| emit(&mut intermediates, ctrl.clone(), a, b) );
ctrl_clone.send(mapper_done);
}

View File

@ -10,11 +10,13 @@
#[feature(managed_boxes)];
use std::cell::Cell;
// Resources can't be copied, but storing into data structures counts
// as a move unless the stored thing is used afterwards.
struct r {
i: @mut int,
i: @Cell<int>,
}
struct Box { x: r }
@ -22,30 +24,30 @@ struct Box { x: r }
#[unsafe_destructor]
impl Drop for r {
fn drop(&mut self) {
*(self.i) = *(self.i) + 1;
self.i.set(self.i.get() + 1)
}
}
fn r(i: @mut int) -> r {
fn r(i: @Cell<int>) -> r {
r {
i: i
}
}
fn test_box() {
let i = @mut 0;
let i = @Cell::new(0);
{
let _a = @r(i);
}
assert_eq!(*i, 1);
assert_eq!(i.get(), 1);
}
fn test_rec() {
let i = @mut 0;
let i = @Cell::new(0);
{
let _a = Box {x: r(i)};
}
assert_eq!(*i, 1);
assert_eq!(i.get(), 1);
}
fn test_tag() {
@ -53,37 +55,37 @@ fn test_tag() {
t0(r),
}
let i = @mut 0;
let i = @Cell::new(0);
{
let _a = t0(r(i));
}
assert_eq!(*i, 1);
assert_eq!(i.get(), 1);
}
fn test_tup() {
let i = @mut 0;
let i = @Cell::new(0);
{
let _a = (r(i), 0);
}
assert_eq!(*i, 1);
assert_eq!(i.get(), 1);
}
fn test_unique() {
let i = @mut 0;
let i = @Cell::new(0);
{
let _a = ~r(i);
}
assert_eq!(*i, 1);
assert_eq!(i.get(), 1);
}
fn test_box_rec() {
let i = @mut 0;
let i = @Cell::new(0);
{
let _a = @Box {
x: r(i)
};
}
assert_eq!(*i, 1);
assert_eq!(i.get(), 1);
}
pub fn main() {

View File

@ -16,11 +16,12 @@
extern mod req;
use req::request;
use std::cell::RefCell;
use std::hashmap::HashMap;
pub fn main() {
let v = ~[@~"hi"];
let mut m: req::header_map = HashMap::new();
m.insert(~"METHOD", @mut v);
m.insert(~"METHOD", @RefCell::new(v));
request::<int>(&m);
}

View File

@ -10,26 +10,28 @@
#[feature(managed_boxes)];
use std::cell::Cell;
// This test should behave exactly like issue-2735-3
struct defer {
b: @mut bool,
b: @Cell<bool>,
}
#[unsafe_destructor]
impl Drop for defer {
fn drop(&mut self) {
*self.b = true;
self.b.set(true);
}
}
fn defer(b: @mut bool) -> defer {
fn defer(b: @Cell<bool>) -> defer {
defer {
b: b
}
}
pub fn main() {
let dtor_ran = @mut false;
let dtor_ran = @Cell::new(false);
let _ = defer(dtor_ran);
assert!(*dtor_ran);
assert!(dtor_ran.get());
}

View File

@ -10,26 +10,28 @@
#[feature(managed_boxes)];
use std::cell::Cell;
// This test should behave exactly like issue-2735-2
struct defer {
b: @mut bool,
b: @Cell<bool>,
}
#[unsafe_destructor]
impl Drop for defer {
fn drop(&mut self) {
*self.b = true;
self.b.set(true);
}
}
fn defer(b: @mut bool) -> defer {
fn defer(b: @Cell<bool>) -> defer {
defer {
b: b
}
}
pub fn main() {
let dtor_ran = @mut false;
let dtor_ran = @Cell::new(false);
defer(dtor_ran);
assert!(*dtor_ran);
assert!(dtor_ran.get());
}

View File

@ -62,8 +62,8 @@ fn square_from_char(c: char) -> square {
}
}
fn read_board_grid<rdr:'static + io::Reader>(input: rdr) -> ~[~[square]] {
let input = @mut input as @mut io::Reader;
fn read_board_grid<rdr:'static + io::Reader>(mut input: rdr) -> ~[~[square]] {
let mut input: &mut io::Reader = &mut input;
let mut grid = ~[];
let mut line = [0, ..10];
input.read(line);

View File

@ -10,11 +10,13 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
static S: &'static str = "str";
struct list<T> {
element: T,
next: Option<@mut list<T>>
next: Option<@RefCell<list<T>>>
}
impl<T:'static> list<T> {
@ -24,7 +26,7 @@ impl<T:'static> list<T> {
next: None
};
self.next = Some(@mut newList);
self.next = Some(@RefCell::new(newList));
}
}

View File

@ -1,26 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
struct Foo { x: int }
impl Foo {
pub fn stuff<'a>(&'a mut self) -> &'a mut Foo {
return self;
}
}
pub fn main() {
let x = @mut Foo { x: 3 };
// Neither of the next two lines should cause an error
let _ = x.stuff();
x.stuff();
}

View File

@ -1,31 +0,0 @@
// 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.
#[feature(managed_boxes)];
// Regression test for issue #5275
fn foo(self_: &A) -> int {
if true {
fail!()
} else {
*bar(self_.bar)
}
}
fn bar<'r>(_: &'r mut int) -> &'r int {
fail!()
}
struct A {
bar: @mut int,
}
pub fn main() {}

View File

@ -1,16 +0,0 @@
// 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.
#[feature(managed_boxes)];
pub fn main() {
let box1 = @mut 42;
let _x = *(&mut *box1) == 42 || *(&mut *box1) == 31337;
}

View File

@ -10,28 +10,30 @@
#[feature(managed_boxes)];
use std::cell::Cell;
struct r {
b: @mut int,
b: @Cell<int>,
}
#[unsafe_destructor]
impl Drop for r {
fn drop(&mut self) {
*(self.b) += 1;
self.b.set(self.b.get() + 1);
}
}
fn r(b: @mut int) -> r {
fn r(b: @Cell<int>) -> r {
r {
b: b
}
}
pub fn main() {
let b = @mut 0;
let b = @Cell::new(0);
{
let _p = Some(r(b));
}
assert_eq!(*b, 1);
assert_eq!(b.get(), 1);
}

View File

@ -10,16 +10,20 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
enum maybe_pointy {
no_pointy,
yes_pointy(@mut Pointy),
yes_pointy(@RefCell<Pointy>),
}
struct Pointy {
x : maybe_pointy
x: maybe_pointy
}
pub fn main() {
let m = @mut Pointy { x : no_pointy };
m.x = yes_pointy(m);
let m = @RefCell::new(Pointy { x : no_pointy });
m.set(Pointy {
x: yes_pointy(m)
});
}

View File

@ -11,12 +11,10 @@
// This should typecheck even though the type of e is not fully
// resolved when we finish typechecking the ||.
#[feature(managed_boxes)];
struct Refs { refs: ~[int], n: int }
pub fn main() {
let e = @mut Refs{refs: ~[], n: 0};
let mut e = Refs{refs: ~[], n: 0};
let _f: || = || error!("{}", e.n);
e.refs.push(1);
}

View File

@ -1,36 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
// Tests that shapes respect linearize_ty_params().
enum option<T> {
none,
some(T),
}
struct Smallintmap<T> {v: ~[option<T>]}
struct V<T> { v: ~[option<T>] }
fn mk<T:'static>() -> @mut Smallintmap<T> {
let v: ~[option<T>] = ~[];
return @mut Smallintmap {v: v};
}
fn f<T,U:'static>() {
let sim = mk::<U>();
error!("{:?}", sim);
}
pub fn main() {
f::<int,int>();
}

View File

@ -11,25 +11,24 @@
// xfail-fast
// exec-env:RUST_LOG=debug
#[feature(managed_boxes)];
use std::cell::Cell;
use std::fmt;
struct Foo(@mut int);
struct Foo(Cell<int>);
impl fmt::Default for Foo {
fn fmt(f: &Foo, _fmt: &mut fmt::Formatter) {
assert!(***f == 0);
***f = 1;
assert!(f.get() == 0);
f.set(1);
}
}
pub fn main() {
let (p,c) = Chan::new();
do spawn {
let f = Foo(@mut 0);
let mut f = Foo(Cell::new(0));
debug!("{}", f);
assert!(**f == 1);
assert!(f.get() == 1);
c.send(());
}
p.recv();

View File

@ -1,26 +0,0 @@
// Copyright 2012 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.
// xfail-test
extern mod std;
use std::gc;
use std::gc::rustrt;
struct cell {c: @list}
enum list { link(@mut cell), nil, }
pub fn main() {
let first: @cell = @mut cell{c: @nil()};
let second: @cell = @mut cell{c: @link(first)};
first._0 = @link(second);
rustrt::gc();
let third: @cell = @mut cell{c: @nil()};
}

View File

@ -1,16 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
pub fn main() {
let _x: @mut [int] = @mut [ 1, 2, 3 ];
}

View File

@ -12,19 +12,21 @@
// Make sure the destructor is run for newtype structs.
struct Foo(@mut int);
use std::cell::Cell;
struct Foo(@Cell<int>);
#[unsafe_destructor]
impl Drop for Foo {
fn drop(&mut self) {
***self = 23;
self.set(23);
}
}
pub fn main() {
let y = @mut 32;
let y = @Cell::new(32);
{
let _x = Foo(y);
}
assert_eq!(*y, 23);
assert_eq!(y.get(), 23);
}

View File

@ -14,24 +14,12 @@
trait Foo {
fn foo(&self) -> uint;
fn bar(&mut self) -> uint;
}
impl Foo for uint {
fn foo(&self) -> uint {
*self
}
fn bar(&mut self) -> uint {
*self += 1;
*self
}
}
fn do_it_mut(obj: &mut Foo) {
let x = obj.bar();
let y = obj.foo();
assert_eq!(x, y);
}
fn do_it_imm(obj: &Foo, v: uint) {
@ -40,7 +28,6 @@ fn do_it_imm(obj: &Foo, v: uint) {
}
pub fn main() {
let x = @mut 22u as @mut Foo;
do_it_mut(x);
do_it_imm(x, 23u);
let x = @22u as @Foo;
do_it_imm(x, 22u);
}

View File

@ -10,16 +10,17 @@
#[feature(managed_boxes)];
struct dtor {
x: @mut int,
use std::cell::Cell;
struct dtor {
x: @Cell<int>,
}
#[unsafe_destructor]
impl Drop for dtor {
fn drop(&mut self) {
// abuse access to shared mutable state to write this code
*self.x -= 1;
self.x.set(self.x.get() - 1);
}
}
@ -31,12 +32,12 @@ fn unwrap<T>(o: Option<T>) -> T {
}
pub fn main() {
let x = @mut 1;
let x = @Cell::new(1);
{
let b = Some(dtor { x:x });
let _c = unwrap(b);
}
assert_eq!(*x, 0);
assert_eq!(x.get(), 0);
}

View File

@ -22,6 +22,6 @@ pub fn main() {
assert_eq!(mem::size_of::<S<u64, u16>>(), 11);
assert_eq!(mem::size_of::<S<~str, @mut [int]>>(),
1 + mem::size_of::<~str>() + mem::size_of::<@mut [int]>());
assert_eq!(mem::size_of::<S<~str, @[int]>>(),
1 + mem::size_of::<~str>() + mem::size_of::<@[int]>());
}

View File

@ -49,7 +49,7 @@ struct S7_Option {
a: f32,
b: u8,
c: u16,
d: Option<@mut f64>
d: Option<@f64>
}
// Placing packed structs in statics should work
@ -63,5 +63,5 @@ pub fn main() {
assert_eq!(mem::size_of::<S5>(), 5);
assert_eq!(mem::size_of::<S13_str>(), 13 + mem::size_of::<~str>());
assert_eq!(mem::size_of::<S3_Foo>(), 3 + mem::size_of::<Foo>());
assert_eq!(mem::size_of::<S7_Option>(), 7 + mem::size_of::<Option<@mut f64>>());
assert_eq!(mem::size_of::<S7_Option>(), 7 + mem::size_of::<Option<@f64>>());
}

View File

@ -30,7 +30,7 @@ enum Foo {
struct S3_Foo(u8, u16, Foo);
#[packed]
struct S7_Option(f32, u8, u16, Option<@mut f64>);
struct S7_Option(f32, u8, u16, Option<@f64>);
pub fn main() {
assert_eq!(mem::size_of::<S4>(), 4);
@ -44,5 +44,5 @@ pub fn main() {
3 + mem::size_of::<Foo>());
assert_eq!(mem::size_of::<S7_Option>(),
7 + mem::size_of::<Option<@mut f64>>());
7 + mem::size_of::<Option<@f64>>());
}

View File

@ -24,7 +24,7 @@ impl<'a> get for &'a int {
}
pub fn main() {
let x = @mut 6;
let x = @6;
let y = x.get();
assert_eq!(y, 6);

View File

@ -12,6 +12,7 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
use std::libc::c_void;
use std::ptr;
use std::mem;
@ -458,8 +459,9 @@ impl<V:TyVisitor + movable_ptr> TyVisitor for ptr_visit_adaptor<V> {
}
}
struct my_visitor(@mut Stuff);
struct my_visitor(@RefCell<Stuff>);
#[deriving(Clone)]
struct Stuff {
ptr1: *c_void,
ptr2: *c_void,
@ -469,7 +471,7 @@ struct Stuff {
impl my_visitor {
pub fn get<T:Clone>(&mut self, f: |T|) {
unsafe {
f((*(self.ptr1 as *T)).clone());
f((*((**self).get().ptr1 as *T)).clone());
}
}
@ -487,8 +489,9 @@ struct Inner<V> { inner: V }
impl movable_ptr for my_visitor {
fn move_ptr(&mut self, adjustment: |*c_void| -> *c_void) {
self.ptr1 = adjustment(self.ptr1);
self.ptr2 = adjustment(self.ptr2);
let mut this = self.borrow_mut();
this.get().ptr1 = adjustment(this.get().ptr1);
this.get().ptr2 = adjustment(this.get().ptr2);
}
}
@ -497,11 +500,17 @@ impl TyVisitor for my_visitor {
fn visit_bot(&mut self) -> bool { true }
fn visit_nil(&mut self) -> bool { true }
fn visit_bool(&mut self) -> bool {
self.get::<bool>(|b| self.vals.push(b.to_str()));
self.get::<bool>(|b| {
let mut this = self.borrow_mut();
this.get().vals.push(b.to_str());
});
true
}
fn visit_int(&mut self) -> bool {
self.get::<int>(|i| self.vals.push(i.to_str()));
self.get::<int>(|i| {
let mut this = self.borrow_mut();
this.get().vals.push(i.to_str());
});
true
}
fn visit_i8(&mut self) -> bool { true }
@ -622,21 +631,22 @@ pub fn main() {
unsafe {
let r = (1,2,3,true,false, Triple {x:5,y:4,z:3}, (12,));
let p = ptr::to_unsafe_ptr(&r) as *c_void;
let u = my_visitor(@mut Stuff {ptr1: p,
ptr2: p,
vals: ~[]});
let u = my_visitor(@RefCell::new(Stuff {ptr1: p,
ptr2: p,
vals: ~[]}));
let mut v = ptr_visit_adaptor(Inner {inner: u});
let td = get_tydesc_for(r);
error!("tydesc sz: {}, align: {}",
(*td).size, (*td).align);
visit_tydesc(td, &mut v as &mut TyVisitor);
let r = u.vals.clone();
let mut ub = u.borrow_mut();
let r = ub.get().vals.clone();
for s in r.iter() {
println!("val: {}", *s);
}
error!("{:?}", u.vals.clone());
assert_eq!(u.vals.clone(),
error!("{:?}", ub.get().vals.clone());
assert_eq!(ub.get().vals.clone(),
~[ ~"1", ~"2", ~"3", ~"true", ~"false", ~"5", ~"4", ~"3", ~"12"]);
}
}

View File

@ -13,7 +13,7 @@
use std::unstable::intrinsics::{TyDesc, get_tydesc, visit_tydesc, TyVisitor, Disr, Opaque};
struct MyVisitor {
types: @mut ~[~str],
types: ~[~str],
}
impl TyVisitor for MyVisitor {
@ -153,7 +153,7 @@ fn visit_ty<T>(v: &mut MyVisitor) {
}
pub fn main() {
let mut v = MyVisitor {types: @mut ~[]};
let mut v = MyVisitor {types: ~[]};
visit_ty::<bool>(&mut v);
visit_ty::<int>(&mut v);
@ -164,5 +164,5 @@ pub fn main() {
for s in v.types.iter() {
println!("type: {}", (*s).clone());
}
assert_eq!((*v.types).clone(), ~[~"bool", ~"int", ~"i8", ~"i16", ~"[", ~"int", ~"]"]);
assert_eq!(v.types.clone(), ~[~"bool", ~"int", ~"i8", ~"i16", ~"[", ~"int", ~"]"]);
}

View File

@ -10,25 +10,27 @@
#[feature(managed_boxes)];
use std::cell::Cell;
struct r {
i: @mut int,
i: @Cell<int>,
}
#[unsafe_destructor]
impl Drop for r {
fn drop(&mut self) {
*(self.i) += 1;
self.i.set(self.i.get() + 1);
}
}
fn r(i: @mut int) -> r {
fn r(i: @Cell<int>) -> r {
r {
i: i
}
}
pub fn main() {
let i = @mut 0;
let i = @Cell::new(0);
// Even though these look like copies, they are guaranteed not to be
{
let a = r(i);
@ -36,5 +38,5 @@ pub fn main() {
let (c, _d) = b;
info!("{:?}", c);
}
assert_eq!(*i, 1);
assert_eq!(i.get(), 1);
}

View File

@ -1,83 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
// Don't leak the unique pointers
use std::cast;
struct r {
v: *int,
}
impl Drop for r {
fn drop(&mut self) {
unsafe {
info!("r's dtor: self = {:x}, self.v = {:x}, self.v's value = {:x}",
cast::transmute::<*mut r, uint>(self),
cast::transmute::<**int, uint>(&(self.v)),
cast::transmute::<*int, uint>(self.v));
let _v2: ~int = cast::transmute(self.v);
}
}
}
fn r(v: *int) -> r {
r {
v: v
}
}
struct t(Node);
struct Node {
next: Option<@mut t>,
r: r
}
pub fn main() {
unsafe {
let i1 = ~0;
let i1p = cast::transmute_copy(&i1);
cast::forget(i1);
let i2 = ~0;
let i2p = cast::transmute_copy(&i2);
cast::forget(i2);
let x1 = @mut t(Node{
next: None,
r: {
let rs = r(i1p);
info!("r = {:x}", cast::transmute::<*r, uint>(&rs));
rs }
});
info!("x1 = {:x}, x1.r = {:x}",
cast::transmute::<@mut t, uint>(x1),
cast::transmute::<*r, uint>(&x1.r));
let x2 = @mut t(Node{
next: None,
r: {
let rs = r(i2p);
info!("r2 = {:x}", cast::transmute::<*r, uint>(&rs));
rs
}
});
info!("x2 = {:x}, x2.r = {:x}",
cast::transmute::<@mut t, uint>(x2),
cast::transmute::<*r, uint>(&(x2.r)));
x1.next = Some(x2);
x2.next = Some(x1);
}
}

View File

@ -1,71 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
// Don't leak the unique pointers
use std::cast;
struct U {
a: int,
b: int,
c: *int
}
struct r {
v: U,
}
impl Drop for r {
fn drop(&mut self) {
unsafe {
let _v2: ~int = cast::transmute(self.v.c);
}
}
}
fn r(v: U) -> r {
r {
v: v
}
}
struct t(Node);
struct Node {
next: Option<@mut t>,
r: r
}
pub fn main() {
unsafe {
let i1 = ~0xA;
let i1p = cast::transmute_copy(&i1);
cast::forget(i1);
let i2 = ~0xA;
let i2p = cast::transmute_copy(&i2);
cast::forget(i2);
let u1 = U {a: 0xB, b: 0xC, c: i1p};
let u2 = U {a: 0xB, b: 0xC, c: i2p};
let x1 = @mut t(Node {
next: None,
r: r(u1)
});
let x2 = @mut t(Node {
next: None,
r: r(u2)
});
x1.next = Some(x2);
x2.next = Some(x1);
}
}

View File

@ -1,80 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
// same as resource-cycle2, but be sure to give r multiple fields...
// Don't leak the unique pointers
use std::cast;
struct U {
a: int,
b: int,
c: *int
}
struct R {
v: U,
w: int,
x: *int,
}
impl Drop for R {
fn drop(&mut self) {
unsafe {
let _v2: ~int = cast::transmute(self.v.c);
// let _v3: ~int = cast::transmute_copy(self.x);
}
}
}
fn r(v: U, w: int, _x: *int) -> R {
unsafe {
R {
v: v,
w: w,
x: cast::transmute(0)
}
}
}
struct t(Node);
struct Node {
next: Option<@mut t>,
r: R
}
pub fn main() {
unsafe {
let i1 = ~0xA;
let i1p = cast::transmute_copy(&i1);
cast::forget(i1);
let i2 = ~0xA;
let i2p = cast::transmute_copy(&i2);
cast::forget(i2);
let u1 = U {a: 0xB, b: 0xC, c: i1p};
let u2 = U {a: 0xB, b: 0xC, c: i2p};
let x1 = @mut t(Node{
next: None,
r: r(u1, 42, i1p)
});
let x2 = @mut t(Node{
next: None,
r: r(u2, 42, i2p)
});
x1.next = Some(x2);
x2.next = Some(x1);
}
}

View File

@ -10,30 +10,32 @@
#[feature(managed_boxes)];
use std::cell::Cell;
struct shrinky_pointer {
i: @@mut int,
i: @@Cell<int>,
}
#[unsafe_destructor]
impl Drop for shrinky_pointer {
fn drop(&mut self) {
error!("Hello!"); **(self.i) -= 1;
error!("Hello!"); self.i.set(self.i.get() - 1);
}
}
impl shrinky_pointer {
pub fn look_at(&self) -> int { return **(self.i); }
pub fn look_at(&self) -> int { return self.i.get(); }
}
fn shrinky_pointer(i: @@mut int) -> shrinky_pointer {
fn shrinky_pointer(i: @@Cell<int>) -> shrinky_pointer {
shrinky_pointer {
i: i
}
}
pub fn main() {
let my_total = @@mut 10;
let my_total = @@Cell::new(10);
{ let pt = shrinky_pointer(my_total); assert!((pt.look_at() == 10)); }
error!("my_total = {}", **my_total);
assert_eq!(**my_total, 9);
error!("my_total = {}", my_total.get());
assert_eq!(my_total.get(), 9);
}

View File

@ -13,7 +13,9 @@
// Ensures that class dtors run if the object is inside an enum
// variant
type closable = @mut bool;
use std::cell::Cell;
type closable = @Cell<bool>;
struct close_res {
i: closable,
@ -23,7 +25,7 @@ struct close_res {
#[unsafe_destructor]
impl Drop for close_res {
fn drop(&mut self) {
*(self.i) = false;
self.i.set(false);
}
}
@ -38,8 +40,8 @@ enum option<T> { none, some(T), }
fn sink(_res: option<close_res>) { }
pub fn main() {
let c = @mut true;
let c = @Cell::new(true);
sink(none);
sink(some(close_res(c)));
assert!((!*c));
assert!(!c.get());
}

View File

@ -13,12 +13,14 @@
// A port of task-killjoin to use a class with a dtor to manage
// the join.
use std::cell::Cell;
use std::comm::*;
use std::ptr;
use std::task;
struct notify {
ch: Chan<bool>, v: @mut bool,
ch: Chan<bool>,
v: @Cell<bool>,
}
#[unsafe_destructor]
@ -36,7 +38,7 @@ impl Drop for notify {
}
}
fn notify(ch: Chan<bool>, v: @mut bool) -> notify {
fn notify(ch: Chan<bool>, v: @Cell<bool>) -> notify {
notify {
ch: ch,
v: v
@ -45,10 +47,10 @@ fn notify(ch: Chan<bool>, v: @mut bool) -> notify {
fn joinable(f: proc()) -> Port<bool> {
fn wrapper(c: Chan<bool>, f: ||) {
let b = @mut false;
let b = @Cell::new(false);
error!("wrapper: task=%? allocated v=%x",
0,
ptr::to_unsafe_ptr(&(*b)) as uint);
ptr::to_unsafe_ptr(&b) as uint);
let _r = notify(c, b);
f();
*b = true;

View File

@ -12,7 +12,9 @@
// Test cyclic detector when using trait instances.
struct Tree(@mut TreeR);
use std::cell::RefCell;
struct Tree(@RefCell<TreeR>);
struct TreeR {
left: Option<Tree>,
right: Option<Tree>,
@ -38,8 +40,9 @@ impl to_str for int {
impl to_str for Tree {
fn to_str_(&self) -> ~str {
let (l, r) = (self.left, self.right);
let val = &self.val;
let this = self.borrow();
let (l, r) = (this.get().left, this.get().right);
let val = &this.get().val;
format!("[{}, {}, {}]", val.to_str_(), l.to_str_(), r.to_str_())
}
}
@ -47,14 +50,18 @@ impl to_str for Tree {
fn foo<T:to_str>(x: T) -> ~str { x.to_str_() }
pub fn main() {
let t1 = Tree(@mut TreeR{left: None,
right: None,
val: ~1 as ~to_str });
let t2 = Tree(@mut TreeR{left: Some(t1),
right: Some(t1),
val: ~2 as ~to_str });
let t1 = Tree(@RefCell::new(TreeR{left: None,
right: None,
val: ~1 as ~to_str}));
let t2 = Tree(@RefCell::new(TreeR{left: Some(t1),
right: Some(t1),
val: ~2 as ~to_str}));
let expected = ~"[2, some([1, none, none]), some([1, none, none])]";
assert!(t2.to_str_() == expected);
assert!(foo(t2) == expected);
t1.left = Some(t2); // create cycle
{
let mut t1 = t1.borrow_mut();
t1.get().left = Some(t2); // create cycle
}
}

View File

@ -10,11 +10,12 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
use std::ptr;
enum maybe_pointy {
none,
p(@mut Pointy),
p(@RefCell<Pointy>),
}
struct Pointy {
@ -27,14 +28,17 @@ fn make_uniq_closure<A:Send>(a: A) -> proc() -> uint {
result
}
fn empty_pointy() -> @mut Pointy {
return @mut Pointy {
fn empty_pointy() -> @RefCell<Pointy> {
return @RefCell::new(Pointy {
a : none,
d : make_uniq_closure(~"hi")
}
})
}
pub fn main() {
let v = empty_pointy();
v.a = p(v);
{
let mut vb = v.borrow_mut();
vb.get().a = p(v);
}
}

View File

@ -10,9 +10,11 @@
#[feature(managed_boxes)];
use std::cell::RefCell;
enum maybe_pointy {
none,
p(@mut Pointy),
p(@RefCell<Pointy>),
}
struct Pointy {
@ -21,15 +23,18 @@ struct Pointy {
d : proc()->(),
}
fn empty_pointy() -> @mut Pointy {
return @mut Pointy {
fn empty_pointy() -> @RefCell<Pointy> {
return @RefCell::new(Pointy {
a : none,
c : ~22,
d : proc() {},
}
})
}
pub fn main() {
let v = empty_pointy();
v.a = p(v);
{
let mut vb = v.borrow_mut();
vb.get().a = p(v);
}
}

View File

@ -1,36 +0,0 @@
// Copyright 2012 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.
#[feature(managed_boxes)];
struct r {
i: @mut int,
}
#[unsafe_destructor]
impl Drop for r {
fn drop(&mut self) {
*(self.i) = *(self.i) + 1;
}
}
fn r(i: @mut int) -> r {
r {
i: i
}
}
pub fn main() {
let i = @mut 0;
{
let _j = ~r(i);
}
assert_eq!(*i, 1);
}

View File

@ -10,29 +10,31 @@
#[feature(managed_boxes)];
use std::cell::Cell;
// Make sure that destructors get run on slice literals
struct foo {
x: @mut int,
x: @Cell<int>,
}
#[unsafe_destructor]
impl Drop for foo {
fn drop(&mut self) {
*self.x += 1;
self.x.set(self.x.get() + 1);
}
}
fn foo(x: @mut int) -> foo {
fn foo(x: @Cell<int>) -> foo {
foo {
x: x
}
}
pub fn main() {
let x = @mut 0;
let x = @Cell::new(0);
{
let l = &[foo(x)];
assert_eq!(*l[0].x, 0);
assert_eq!(l[0].x.get(), 0);
}
assert_eq!(*x, 1);
assert_eq!(x.get(), 1);
}

View File

@ -10,6 +10,7 @@
#[feature(managed_boxes)];
use std::cell::Cell;
use std::util;
// Just a grab bag of stuff that you wouldn't want to actually write.
@ -22,11 +23,13 @@ fn funny() {
}
fn what() {
fn the(x: @mut bool) { return while !*x { *x = true; }; }
let i = @mut false;
fn the(x: @Cell<bool>) {
return while !x.get() { x.set(true); };
}
let i = @Cell::new(false);
let dont = {||the(i)};
dont();
assert!((*i));
assert!((i.get()));
}
fn zombiejesus() {