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-03-22 15:13:15 -05:00
|
|
|
|
2015-01-07 19:25:56 -06:00
|
|
|
#![allow(unknown_features)]
|
2015-01-07 13:32:25 -06:00
|
|
|
#![feature(box_syntax)]
|
2015-02-10 15:52:00 -06:00
|
|
|
#![feature(box_patterns)]
|
2015-01-05 00:28:53 -06:00
|
|
|
#![feature(unboxed_closures)]
|
2014-11-02 17:58:00 -06:00
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
struct X(Box<isize>);
|
2014-11-02 17:58:00 -06:00
|
|
|
|
|
|
|
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 {
|
2015-03-25 19:06:52 -05:00
|
|
|
type Target = isize;
|
2015-01-01 13:53:20 -06:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn deref(&self) -> &isize {
|
2014-11-02 17:58:00 -06:00
|
|
|
let &X(box ref x) = self;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:53:20 -06:00
|
|
|
impl DerefMut for X {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn deref_mut(&mut self) -> &mut isize {
|
2015-01-07 18:26:00 -06:00
|
|
|
let &mut X(box ref mut x) = self;
|
2014-11-02 17:58:00 -06:00
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut test = X(box 5);
|
2014-11-02 17:58:00 -06:00
|
|
|
{
|
2015-02-01 11:44:15 -06:00
|
|
|
let mut change = || { *test = 10 };
|
2014-11-02 17:58:00 -06:00
|
|
|
change();
|
|
|
|
}
|
|
|
|
assert_eq!(*test, 10);
|
|
|
|
}
|
|
|
|
assert!(unsafe { DESTRUCTOR_RAN });
|
|
|
|
}
|