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