]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/overloaded-deref.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / test / run-pass / overloaded-deref.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(collections)]
12
13 use std::cell::RefCell;
14 use std::rc::Rc;
15 use std::string::String;
16
17 #[derive(PartialEq, Debug)]
18 struct Point {
19 x: isize,
20 y: isize
21 }
22
23 pub fn main() {
24 assert_eq!(*Rc::new(5), 5);
25 assert_eq!(***Rc::new(Box::new(Box::new(5))), 5);
26 assert_eq!(*Rc::new(Point {x: 2, y: 4}), Point {x: 2, y: 4});
27
28 let i = Rc::new(RefCell::new(2));
29 let i_value = *(*i).borrow();
30 *(*i).borrow_mut() = 5;
31 assert_eq!((i_value, *(*i).borrow()), (2, 5));
32
33 let s = Rc::new("foo".to_string());
34 assert_eq!(*s, "foo".to_string());
35 assert_eq!((*s), "foo");
36
37 let mut_s = Rc::new(RefCell::new(String::from("foo")));
38 (*(*mut_s).borrow_mut()).push_str("bar");
39 // assert_eq! would panic here because it stores the LHS and RHS in two locals.
40 assert_eq!((*(*mut_s).borrow()), "foobar");
41 assert_eq!((*(*mut_s).borrow_mut()), "foobar");
42
43 let p = Rc::new(RefCell::new(Point {x: 1, y: 2}));
44 (*(*p).borrow_mut()).x = 3;
45 (*(*p).borrow_mut()).y += 3;
46 assert_eq!(*(*p).borrow(), Point {x: 3, y: 5});
47
48 let v = Rc::new(RefCell::new(vec!(1, 2, 3)));
49 (*(*v).borrow_mut())[0] = 3;
50 (*(*v).borrow_mut())[1] += 3;
51 assert_eq!(((*(*v).borrow())[0],
52 (*(*v).borrow())[1],
53 (*(*v).borrow())[2]), (3, 5, 3));
54 }