]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/overloaded-deref-count.rs
librustc: Remove the fallback to `int` from typechecking.
[rust.git] / src / test / run-pass / overloaded-deref-count.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 use std::cell::Cell;
12 use std::ops::{Deref, DerefMut};
13 use std::vec::Vec;
14
15 struct DerefCounter<T> {
16     count_imm: Cell<uint>,
17     count_mut: uint,
18     value: T
19 }
20
21 impl<T> DerefCounter<T> {
22     fn new(value: T) -> DerefCounter<T> {
23         DerefCounter {
24             count_imm: Cell::new(0),
25             count_mut: 0,
26             value: value
27         }
28     }
29
30     fn counts(&self) -> (uint, uint) {
31         (self.count_imm.get(), self.count_mut)
32     }
33 }
34
35 impl<T> Deref<T> for DerefCounter<T> {
36     fn deref<'a>(&'a self) -> &'a T {
37         self.count_imm.set(self.count_imm.get() + 1);
38         &self.value
39     }
40 }
41
42 impl<T> DerefMut<T> for DerefCounter<T> {
43     fn deref_mut<'a>(&'a mut self) -> &'a mut T {
44         self.count_mut += 1;
45         &mut self.value
46     }
47 }
48
49 pub fn main() {
50     let mut n = DerefCounter::new(0i);
51     let mut v = DerefCounter::new(Vec::new());
52
53     let _ = *n; // Immutable deref + copy a POD.
54     assert_eq!(n.counts(), (1, 0));
55
56     let _ = (&*n, &*v); // Immutable deref + borrow.
57     assert_eq!(n.counts(), (2, 0)); assert_eq!(v.counts(), (1, 0));
58
59     let _ = (&mut *n, &mut *v); // Mutable deref + mutable borrow.
60     assert_eq!(n.counts(), (2, 1)); assert_eq!(v.counts(), (1, 1));
61
62     let mut v2 = Vec::new();
63     v2.push(1i);
64
65     *n = 5; *v = v2; // Mutable deref + assignment.
66     assert_eq!(n.counts(), (2, 2)); assert_eq!(v.counts(), (1, 2));
67
68     *n -= 3; // Mutable deref + assignment with binary operation.
69     assert_eq!(n.counts(), (2, 3));
70
71     // Mutable deref used for calling a method taking &self.
72     // N.B. This is required because method lookup hasn't been performed so
73     // we don't know whether the called method takes mutable self, before
74     // the dereference itself is type-checked (a chicken-and-egg problem).
75     (*n).to_str();
76     assert_eq!(n.counts(), (2, 4));
77
78     // Mutable deref used for calling a method taking &mut self.
79     (*v).push(2);
80     assert_eq!(v.counts(), (1, 3));
81
82     // Check the final states.
83     assert_eq!(*n, 2);
84     assert_eq!((*v).as_slice(), &[1, 2]);
85 }