]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-16774.rs
6ef4f868d215094faf76c687bd6be2ec5949d4d2
[rust.git] / src / test / run-pass / issue-16774.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(unboxed_closures)]
12
13 use std::ops::{Deref, DerefMut};
14
15 struct X(Box<int>);
16
17 static mut DESTRUCTOR_RAN: bool = false;
18
19 impl Drop for X {
20     fn drop(&mut self) {
21         unsafe {
22             assert!(!DESTRUCTOR_RAN);
23             DESTRUCTOR_RAN = true;
24         }
25     }
26 }
27
28 impl Deref for X {
29     type Target = int;
30
31     fn deref(&self) -> &int {
32         let &X(box ref x) = self;
33         x
34     }
35 }
36
37 impl DerefMut for X {
38     fn deref_mut(&mut self) -> &mut int {
39         let &X(box ref mut x) = self;
40         x
41     }
42 }
43
44 fn main() {
45     {
46         let mut test = X(box 5i);
47         {
48             let mut change = |&mut:| { *test = 10 };
49             change();
50         }
51         assert_eq!(*test, 10);
52     }
53     assert!(unsafe { DESTRUCTOR_RAN });
54 }