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.
|
|
|
|
|
|
|
|
use std::task;
|
2014-12-23 13:53:35 -06:00
|
|
|
use std::sync::mpsc::{channel, Sender};
|
2014-06-14 08:55:55 -05:00
|
|
|
|
2014-12-30 22:32:49 -06:00
|
|
|
#[derive(PartialEq, Show)]
|
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 {
|
2014-11-06 02:05:53 -06:00
|
|
|
&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
|
|
|
}
|
2014-11-06 02:05:53 -06:00
|
|
|
&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
|
|
|
}
|
2014-11-06 02:05:53 -06:00
|
|
|
&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();
|
|
|
|
{
|
2014-11-06 02:05:53 -06:00
|
|
|
let v = Foo::NestedVariant(box 42u, 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();
|
2014-11-26 07:12:18 -06:00
|
|
|
task::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);
|
2014-06-14 08:55:55 -05:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
{
|
2014-11-26 07:12:18 -06:00
|
|
|
task::spawn(move|| {
|
2014-11-06 02:05:53 -06:00
|
|
|
let mut v = Foo::NestedVariant(box 42u, SendOnDrop {
|
2014-06-14 08:55:55 -05:00
|
|
|
sender: sender.clone()
|
|
|
|
}, sender.clone());
|
2014-11-06 02:05:53 -06:00
|
|
|
v = Foo::NestedVariant(box 42u, SendOnDrop { sender: sender.clone() }, sender.clone());
|
|
|
|
v = Foo::SimpleVariant(sender.clone());
|
|
|
|
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::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);
|
2014-06-14 08:55:55 -05:00
|
|
|
}
|