2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2015-02-10 21:52:00 +00:00
|
|
|
#![feature(box_patterns)]
|
2014-11-02 23:58:00 +00:00
|
|
|
|
2014-12-22 17:04:23 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
struct X(Box<isize>);
|
2014-11-02 23:58:00 +00: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 19:53:20 +00:00
|
|
|
impl Deref for X {
|
2015-03-26 00:06:52 +00:00
|
|
|
type Target = isize;
|
2015-01-01 19:53:20 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn deref(&self) -> &isize {
|
2014-11-02 23:58:00 +00:00
|
|
|
let &X(box ref x) = self;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:53:20 +00:00
|
|
|
impl DerefMut for X {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut isize {
|
2015-01-08 00:26:00 +00:00
|
|
|
let &mut X(box ref mut x) = self;
|
2014-11-02 23:58:00 +00:00
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
2022-07-07 02:36:10 +00:00
|
|
|
let mut test = X(Box::new(5));
|
2014-11-02 23:58:00 +00:00
|
|
|
{
|
2015-02-01 17:44:15 +00:00
|
|
|
let mut change = || { *test = 10 };
|
2014-11-02 23:58:00 +00:00
|
|
|
change();
|
|
|
|
}
|
|
|
|
assert_eq!(*test, 10);
|
|
|
|
}
|
|
|
|
assert!(unsafe { DESTRUCTOR_RAN });
|
|
|
|
}
|