2012-12-10 17:32:48 -08:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2014-07-26 21:05:03 -07:00
|
|
|
#![feature(unsafe_destructor)]
|
2013-10-23 04:49:18 -04:00
|
|
|
|
2013-12-31 15:46:27 -08:00
|
|
|
use std::cell::Cell;
|
2014-06-11 19:33:52 -07:00
|
|
|
use std::gc::{GC, Gc};
|
2012-08-24 11:04:07 -07:00
|
|
|
|
2013-12-31 15:46:27 -08:00
|
|
|
struct dtor {
|
2014-06-11 19:33:52 -07:00
|
|
|
x: Gc<Cell<int>>,
|
2012-11-14 01:22:37 -05:00
|
|
|
}
|
|
|
|
|
2013-03-20 18:18:57 -07:00
|
|
|
#[unsafe_destructor]
|
2013-02-14 11:47:00 -08:00
|
|
|
impl Drop for dtor {
|
2013-09-16 21:18:07 -04:00
|
|
|
fn drop(&mut self) {
|
2012-08-24 11:04:07 -07:00
|
|
|
// abuse access to shared mutable state to write this code
|
2013-12-31 15:46:27 -08:00
|
|
|
self.x.set(self.x.get() - 1);
|
2012-08-24 11:04:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:33:31 -07:00
|
|
|
fn unwrap<T>(o: Option<T>) -> T {
|
2013-02-15 02:44:18 -08:00
|
|
|
match o {
|
|
|
|
Some(v) => v,
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!()
|
2012-08-24 11:04:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2014-06-11 19:33:52 -07:00
|
|
|
let x = box(GC) Cell::new(1);
|
2012-08-24 11:04:07 -07:00
|
|
|
|
|
|
|
{
|
2012-08-20 12:23:37 -07:00
|
|
|
let b = Some(dtor { x:x });
|
2013-08-17 08:37:42 -07:00
|
|
|
let _c = unwrap(b);
|
2012-08-24 11:04:07 -07:00
|
|
|
}
|
|
|
|
|
2013-12-31 15:46:27 -08:00
|
|
|
assert_eq!(x.get(), 0);
|
2012-11-14 01:22:37 -05:00
|
|
|
}
|