2014-11-02 17:58:00 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-01-05 00:28:53 -06:00
|
|
|
#![feature(unboxed_closures)]
|
2015-01-07 13:32:25 -06:00
|
|
|
#![feature(box_syntax)]
|
2014-11-02 17:58:00 -06:00
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
2014-11-02 17:58:00 -06:00
|
|
|
struct X(Box<int>);
|
|
|
|
|
|
|
|
static mut DESTRUCTOR_RAN: bool = false;
|
|
|
|
|
|
|
|
impl Drop for X {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
assert!(!DESTRUCTOR_RAN);
|
|
|
|
DESTRUCTOR_RAN = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:53:20 -06:00
|
|
|
impl Deref for X {
|
|
|
|
type Target = int;
|
|
|
|
|
2014-11-02 17:58:00 -06:00
|
|
|
fn deref(&self) -> &int {
|
|
|
|
let &X(box ref x) = self;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:53:20 -06:00
|
|
|
impl DerefMut for X {
|
2014-11-02 17:58:00 -06:00
|
|
|
fn deref_mut(&mut self) -> &mut int {
|
|
|
|
let &X(box ref mut x) = self;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
let mut test = X(box 5i);
|
|
|
|
{
|
|
|
|
let mut change = |&mut:| { *test = 10 };
|
|
|
|
change();
|
|
|
|
}
|
|
|
|
assert_eq!(*test, 10);
|
|
|
|
}
|
|
|
|
assert!(unsafe { DESTRUCTOR_RAN });
|
|
|
|
}
|