2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_assignments)]
|
|
|
|
#![allow(unused_variables)]
|
2016-08-08 23:39:37 +00:00
|
|
|
// ignore-emscripten no threads support
|
2021-12-03 15:32:51 +00:00
|
|
|
// needs-unwind
|
2016-08-08 23:39:37 +00:00
|
|
|
|
2015-02-18 03:00:20 +00:00
|
|
|
use std::thread;
|
2014-12-23 19:53:35 +00:00
|
|
|
use std::sync::mpsc::{channel, Sender};
|
2014-06-14 13:55:55 +00:00
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(PartialEq, Debug)]
|
2014-06-14 13:55:55 +00:00
|
|
|
enum Message {
|
|
|
|
Dropped,
|
|
|
|
DestructorRan
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SendOnDrop {
|
|
|
|
sender: Sender<Message>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for SendOnDrop {
|
|
|
|
fn drop(&mut self) {
|
2014-12-23 19:53:35 +00:00
|
|
|
self.sender.send(Message::Dropped).unwrap();
|
2014-06-14 13:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Foo {
|
|
|
|
SimpleVariant(Sender<Message>),
|
2015-03-26 00:06:52 +00:00
|
|
|
NestedVariant(Box<usize>, SendOnDrop, Sender<Message>),
|
2014-06-14 13:55:55 +00:00
|
|
|
FailingVariant { on_drop: SendOnDrop }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Foo {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match self {
|
2015-01-08 00:26:00 +00:00
|
|
|
&mut Foo::SimpleVariant(ref mut sender) => {
|
2014-12-23 19:53:35 +00:00
|
|
|
sender.send(Message::DestructorRan).unwrap();
|
2014-06-14 13:55:55 +00:00
|
|
|
}
|
2015-01-08 00:26:00 +00:00
|
|
|
&mut Foo::NestedVariant(_, _, ref mut sender) => {
|
2014-12-23 19:53:35 +00:00
|
|
|
sender.send(Message::DestructorRan).unwrap();
|
2014-06-14 13:55:55 +00:00
|
|
|
}
|
2015-01-08 00:26:00 +00:00
|
|
|
&mut Foo::FailingVariant { .. } => {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("Failed");
|
2014-06-14 13:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
{
|
2014-11-06 08:05:53 +00:00
|
|
|
let v = Foo::SimpleVariant(sender);
|
2014-06-14 13:55:55 +00:00
|
|
|
}
|
2014-12-23 19:53:35 +00:00
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
|
|
|
|
assert_eq!(receiver.recv().ok(), None);
|
2014-06-14 13:55:55 +00:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
{
|
2021-08-25 00:39:40 +00:00
|
|
|
let v = Foo::NestedVariant(Box::new(42), SendOnDrop { sender: sender.clone() }, sender);
|
2014-06-14 13:55:55 +00:00
|
|
|
}
|
2014-12-23 19:53:35 +00:00
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
|
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::Dropped);
|
|
|
|
assert_eq!(receiver.recv().ok(), None);
|
2014-06-14 13:55:55 +00:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
2015-02-18 03:00:20 +00:00
|
|
|
let t = thread::spawn(move|| {
|
2014-11-06 08:05:53 +00:00
|
|
|
let v = Foo::FailingVariant { on_drop: SendOnDrop { sender: sender } };
|
2014-06-14 13:55:55 +00:00
|
|
|
});
|
2014-12-23 19:53:35 +00:00
|
|
|
assert_eq!(receiver.recv().unwrap(), Message::Dropped);
|
|
|
|
assert_eq!(receiver.recv().ok(), None);
|
2015-02-18 03:00:20 +00:00
|
|
|
drop(t.join());
|
2014-06-14 13:55:55 +00:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
2015-02-18 03:00:20 +00:00
|
|
|
let t = {
|
|
|
|
thread::spawn(move|| {
|
2021-08-25 00:39:40 +00:00
|
|
|
let mut v = Foo::NestedVariant(Box::new(42), SendOnDrop {
|
2014-06-14 13:55:55 +00:00
|
|
|
sender: sender.clone()
|
|
|
|
}, sender.clone());
|
2021-08-25 00:39:40 +00:00
|
|
|
v = Foo::NestedVariant(Box::new(42),
|
2015-02-18 14:05:34 +00:00
|
|
|
SendOnDrop { sender: sender.clone() },
|
|
|
|
sender.clone());
|
2014-11-06 08:05:53 +00:00
|
|
|
v = Foo::SimpleVariant(sender.clone());
|
|
|
|
v = Foo::FailingVariant { on_drop: SendOnDrop { sender: sender } };
|
2015-01-02 07:53:35 +00:00
|
|
|
})
|
|
|
|
};
|
2014-12-23 19:53:35 +00: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-18 03:00:20 +00:00
|
|
|
drop(t.join());
|
2014-06-14 13:55:55 +00:00
|
|
|
}
|