summaryrefslogtreecommitdiffstats
path: root/src/test/ui/drop/drop-trait-enum.rs
blob: d2b77650a9d5c5db8601b6a9a2d13acfc1f624c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// run-pass
#![allow(dead_code)]
#![allow(unused_assignments)]
#![allow(unused_variables)]
// ignore-emscripten no threads support
// needs-unwind

use std::thread;
use std::sync::mpsc::{channel, Sender};

#[derive(PartialEq, Debug)]
enum Message {
    Dropped,
    DestructorRan
}

struct SendOnDrop {
    sender: Sender<Message>
}

impl Drop for SendOnDrop {
    fn drop(&mut self) {
        self.sender.send(Message::Dropped).unwrap();
    }
}

enum Foo {
    SimpleVariant(Sender<Message>),
    NestedVariant(Box<usize>, SendOnDrop, Sender<Message>),
    FailingVariant { on_drop: SendOnDrop }
}

impl Drop for Foo {
    fn drop(&mut self) {
        match self {
            &mut Foo::SimpleVariant(ref mut sender) => {
                sender.send(Message::DestructorRan).unwrap();
            }
            &mut Foo::NestedVariant(_, _, ref mut sender) => {
                sender.send(Message::DestructorRan).unwrap();
            }
            &mut Foo::FailingVariant { .. } => {
                panic!("Failed");
            }
        }
    }
}

pub fn main() {
    let (sender, receiver) = channel();
    {
        let v = Foo::SimpleVariant(sender);
    }
    assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
    assert_eq!(receiver.recv().ok(), None);

    let (sender, receiver) = channel();
    {
        let v = Foo::NestedVariant(Box::new(42), SendOnDrop { sender: sender.clone() }, sender);
    }
    assert_eq!(receiver.recv().unwrap(), Message::DestructorRan);
    assert_eq!(receiver.recv().unwrap(), Message::Dropped);
    assert_eq!(receiver.recv().ok(), None);

    let (sender, receiver) = channel();
    let t = thread::spawn(move|| {
        let v = Foo::FailingVariant { on_drop: SendOnDrop { sender: sender } };
    });
    assert_eq!(receiver.recv().unwrap(), Message::Dropped);
    assert_eq!(receiver.recv().ok(), None);
    drop(t.join());

    let (sender, receiver) = channel();
    let t = {
        thread::spawn(move|| {
            let mut v = Foo::NestedVariant(Box::new(42), SendOnDrop {
                sender: sender.clone()
            }, sender.clone());
            v = Foo::NestedVariant(Box::new(42),
                                   SendOnDrop { sender: sender.clone() },
                                   sender.clone());
            v = Foo::SimpleVariant(sender.clone());
            v = Foo::FailingVariant { on_drop: SendOnDrop { sender: sender } };
        })
    };
    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);
    drop(t.join());
}