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