File: send_between_threads.rs

package info (click to toggle)
rust-snafu 0.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 724 kB
  • sloc: sh: 15; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,018 bytes parent folder | download | duplicates (3)
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
#![cfg(feature="std")]
// This test asserts that errors can be used across threads.

use std::thread;

use snafu::prelude::*;

#[derive(Debug, Snafu)]
enum InnerError {
    Boom,
}

#[derive(Debug, Snafu)]
enum Error {
    Leaf {
        name: String,
    },

    Wrapper {
        source: InnerError,
    },

    BoxedWrapper {
        source: Box<InnerError>,
    },

    BoxedTraitObjectSend {
        source: Box<dyn std::error::Error + Send + 'static>,
    },

    BoxedTraitObjectSendSync {
        source: Box<dyn std::error::Error + Send + Sync + 'static>,
    },
}

fn example() -> Result<(), Error> {
    BoomSnafu.fail().context(WrapperSnafu)
}

#[test]
fn implements_thread_safe_error() {
    fn check_error<E: std::error::Error>() {}
    check_error::<InnerError>();
    check_error::<Error>();

    fn check_send<E: Send>() {}
    check_send::<InnerError>();
    check_send::<Error>();

    let t = thread::spawn(move || example());

    let v = t.join().expect("Thread panicked");
    v.unwrap_err();
}