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