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.
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.
12 use std::ops::{Deref, DerefMut};
15 struct DerefCounter<T> {
16 count_imm: Cell<uint>,
21 impl<T> DerefCounter<T> {
22 fn new(value: T) -> DerefCounter<T> {
24 count_imm: Cell::new(0),
30 fn counts(&self) -> (uint, uint) {
31 (self.count_imm.get(), self.count_mut)
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);
42 impl<T> DerefMut<T> for DerefCounter<T> {
43 fn deref_mut<'a>(&'a mut self) -> &'a mut T {
50 let mut n = DerefCounter::new(0i);
51 let mut v = DerefCounter::new(Vec::new());
53 let _ = *n; // Immutable deref + copy a POD.
54 assert_eq!(n.counts(), (1, 0));
56 let _ = (&*n, &*v); // Immutable deref + borrow.
57 assert_eq!(n.counts(), (2, 0)); assert_eq!(v.counts(), (1, 0));
59 let _ = (&mut *n, &mut *v); // Mutable deref + mutable borrow.
60 assert_eq!(n.counts(), (2, 1)); assert_eq!(v.counts(), (1, 1));
62 let mut v2 = Vec::new();
65 *n = 5; *v = v2; // Mutable deref + assignment.
66 assert_eq!(n.counts(), (2, 2)); assert_eq!(v.counts(), (1, 2));
68 *n -= 3; // Mutable deref + assignment with binary operation.
69 assert_eq!(n.counts(), (2, 3));
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).
76 assert_eq!(n.counts(), (2, 4));
78 // Mutable deref used for calling a method taking &mut self.
80 assert_eq!(v.counts(), (1, 3));
82 // Check the final states.
84 assert_eq!((*v).as_slice(), &[1, 2]);