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

60 lines
1.3 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.
// pretty-expanded FIXME #23616
#![allow(unknown_features)]
#![feature(box_syntax)]
#![feature(box_patterns)]
#![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 = || { *test = 10 };
2014-11-02 17:58:00 -06:00
change();
}
assert_eq!(*test, 10);
}
assert!(unsafe { DESTRUCTOR_RAN });
}