]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/overloaded-autoderef.rs
Auto merge of #20613 - dgriffen:master, r=alexcrichton
[rust.git] / src / test / run-pass / overloaded-autoderef.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 #![allow(unknown_features)]
12 #![feature(box_syntax)]
13
14 use std::cell::RefCell;
15 use std::rc::Rc;
16 use std::num::ToPrimitive;
17
18 #[derive(PartialEq, Show)]
19 struct Point {
20     x: int,
21     y: int
22 }
23
24 pub fn main() {
25     assert_eq!(Rc::new(5u).to_uint(), Some(5));
26     assert_eq!((box &box &Rc::new(box box &box 5u)).to_uint(), Some(5));
27     let point = Rc::new(Point {x: 2, y: 4});
28     assert_eq!(point.x, 2);
29     assert_eq!(point.y, 4);
30
31     let i = Rc::new(RefCell::new(2i));
32     let i_value = *i.borrow();
33     *i.borrow_mut() = 5;
34     assert_eq!((i_value, *i.borrow()), (2, 5));
35
36     let s = Rc::new("foo".to_string());
37     assert_eq!(s.as_slice(), "foo");
38
39     let mut_s = Rc::new(RefCell::new(String::from_str("foo")));
40     mut_s.borrow_mut().push_str("bar");
41     // HACK assert_eq! would panic here because it stores the LHS and RHS in two locals.
42     assert!(mut_s.borrow().as_slice() == "foobar");
43     assert!(mut_s.borrow_mut().as_slice() == "foobar");
44
45     let p = Rc::new(RefCell::new(Point {x: 1, y: 2}));
46     p.borrow_mut().x = 3;
47     p.borrow_mut().y += 3;
48     assert_eq!(*p.borrow(), Point {x: 3, y: 5});
49
50     let v = Rc::new(RefCell::new([1i, 2, 3]));
51     v.borrow_mut()[0] = 3;
52     v.borrow_mut()[1] += 3;
53     assert_eq!((v.borrow()[0], v.borrow()[1], v.borrow()[2]), (3, 5, 3));
54 }