2014-06-14 08:55:55 -05: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.
|
|
|
|
|
2015-01-07 19:25:56 -06:00
|
|
|
#![allow(unknown_features)]
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
|
2015-02-17 21:00:20 -06:00
|
|
|
use std::thread;
|
2014-12-23 13:53:35 -06:00
|
|
|
use std::sync::mpsc::{channel, Sender};
|
2014-06-14 08:55:55 -05:00
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(PartialEq, Debug)]
|
2014-06-14 08:55:55 -05:00
|
|
|
enum Message {
|
|
|
|
Dropped,
|
|
|
|
DestructorRan
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SendOnDrop {
|
|
|
|
sender: Sender<Message>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for SendOnDrop {
|
|
|
|
fn drop(&mut self) {
|
2014-12-23 13:53:35 -06:00
|
|
|
self.sender.send(Message::Dropped).unwrap();
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Foo {
|
|
|
|
SimpleVariant(Sender<Message>),
|
|
|
|
NestedVariant(Box<uint>, SendOnDrop, Sender<Message>),
|
|
|
|
FailingVariant { on_drop: SendOnDrop }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Foo {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match self {
|
2015-01-07 18:26:00 -06:00
|
|
|
&mut Foo::SimpleVariant(ref mut sender) => {
|
2014-12-23 13:53:35 -06:00
|
|
|
sender.send(Message::DestructorRan).unwrap();
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|
2015-01-07 18:26:00 -06:00
|
|
|
&mut Foo::NestedVariant(_, _, ref mut sender) => {
|
2014-12-23 13:53:35 -06:00
|
|
|
sender.send(Message::DestructorRan).unwrap();
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|
2015-01-07 18:26:00 -06:00
|
|
|
&mut Foo::FailingVariant { .. } => {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("Failed");
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
{
|
2014-11-06 02:05:53 -06:00
|
|
|
let v = Foo::SimpleVariant(sender);
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|
2014-12-23 13:53:35 -06:00
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
|
|
|
|
assert_eq!(receiver.recv().ok(), None);
|
2014-06-14 08:55:55 -05:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
{
|
2015-02-18 04:42:01 -06:00
|
|
|
let v = Foo::NestedVariant(box 42_usize, SendOnDrop { sender: sender.clone() }, sender);
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|
2014-12-23 13:53:35 -06:00
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
|
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::Dropped);
|
|
|
|
assert_eq!(receiver.recv().ok(), None);
|
2014-06-14 08:55:55 -05:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
2015-02-17 21:00:20 -06:00
|
|
|
let t = thread::spawn(move|| {
|
2014-11-06 02:05:53 -06:00
|
|
|
let v = Foo::FailingVariant { on_drop: SendOnDrop { sender: sender } };
|
2014-06-14 08:55:55 -05:00
|
|
|
});
|
2014-12-23 13:53:35 -06:00
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::Dropped);
|
|
|
|
assert_eq!(receiver.recv().ok(), None);
|
2015-02-17 21:00:20 -06:00
|
|
|
drop(t.join());
|
2014-06-14 08:55:55 -05:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
2015-02-17 21:00:20 -06:00
|
|
|
let t = {
|
|
|
|
thread::spawn(move|| {
|
2015-02-18 04:42:01 -06:00
|
|
|
let mut v = Foo::NestedVariant(box 42usize, SendOnDrop {
|
2014-06-14 08:55:55 -05:00
|
|
|
sender: sender.clone()
|
|
|
|
}, sender.clone());
|
2015-02-18 04:42:01 -06:00
|
|
|
v = Foo::NestedVariant(box 42_usize, SendOnDrop { sender: sender.clone() }, sender.clone());
|
2014-11-06 02:05:53 -06:00
|
|
|
v = Foo::SimpleVariant(sender.clone());
|
|
|
|
v = Foo::FailingVariant { on_drop: SendOnDrop { sender: sender } };
|
2015-01-02 01:53:35 -06:00
|
|
|
})
|
|
|
|
};
|
2014-12-23 13:53:35 -06:00
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
|
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::Dropped);
|
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
|
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::Dropped);
|
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
|
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::Dropped);
|
|
|
|
assert_eq!(receiver.recv().ok(), None);
|
2015-02-17 21:00:20 -06:00
|
|
|
drop(t.join());
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|