rust/src/test/run-pass/issue-16774.rs

57 lines
1.2 KiB
Rust
Raw Normal View History

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.
#![allow(unknown_features)]
#![feature(box_syntax)]
#![feature(unboxed_closures)]
2014-11-02 17:58:00 -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 {
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
{
let mut change = |&mut:| { *test = 10 };
change();
}
assert_eq!(*test, 10);
}
assert!(unsafe { DESTRUCTOR_RAN });
}