6
6
#![ feature( reentrant_lock) ]
7
7
#![ feature( rwlock_downgrade) ]
8
8
#![ feature( std_internals) ]
9
+ #![ feature( sync_nonpoison) ]
10
+ #![ feature( nonpoison_mutex) ]
9
11
#![ allow( internal_features) ]
12
+ #![ feature( macro_metavar_expr_concat) ] // For concatenating identifiers in macros.
10
13
11
14
mod barrier;
12
15
mod condvar;
@@ -29,3 +32,55 @@ mod rwlock;
29
32
30
33
#[ path = "../common/mod.rs" ]
31
34
mod common;
35
+
36
+ // A macro that generates two test cases for both the poison and nonpoison locks.
37
+ //
38
+ // To write a test that tests both `poison` and `nonpoison` locks, import any of the types
39
+ // under both `poison` and `nonpoison` using the module name `locks` instead. For example, write
40
+ // `use locks::Mutex;` instead of `use std::sync::poiosn::Mutex`. This will import the correct type
41
+ // for each test variant.
42
+ //
43
+ // Write a test as normal in the `test_body`, but instead of calling `unwrap` on `poison` methods
44
+ // that return a `LockResult` or similar, call the macro `maybe_unwrap!(...)` on the result.
45
+ //
46
+ // For example, call `maybe_unwrap!(mutex.lock())` instead of `mutex.lock().unwrap()` or
47
+ // `maybe_unwrap!(rwlock.read())` instead of `rwlock.read().unwrap()`.
48
+ //
49
+ // For the `poison` types, `maybe_unwrap!` will simply unwrap the `Result` (usually this is a form
50
+ // of `LockResult`, but it could also be other kinds of results). For the `nonpoison` types, it is a
51
+ // no-op.
52
+ //
53
+ // The `poison` test will have the same `name`, but with a suffix of `_unwrap_poisoned`.
54
+ #[ macro_export]
55
+ macro_rules! nonpoison_and_poison_unwrap_test {
56
+ (
57
+ name: $name: ident,
58
+ test_body: { $( $test_body: tt) * }
59
+ ) => {
60
+ // Creates the nonpoison test.
61
+ #[ test]
62
+ fn $name( ) {
63
+ use :: std:: sync:: nonpoison as locks;
64
+
65
+ #[ allow( unused_macros) ]
66
+ macro_rules! maybe_unwrap {
67
+ ( $e: expr) => { $e } ;
68
+ }
69
+
70
+ $( $test_body) *
71
+ }
72
+
73
+ // Creates the poison test with the suffix `_unwrap_poisoned`.
74
+ #[ test]
75
+ fn ${ concat( $name, _unwrap_poisoned) } ( ) {
76
+ use :: std:: sync:: poison as locks;
77
+
78
+ #[ allow( unused_macros) ]
79
+ macro_rules! maybe_unwrap {
80
+ ( $e: expr) => { :: std:: result:: Result :: unwrap( $e) } ;
81
+ }
82
+
83
+ $( $test_body) *
84
+ }
85
+ }
86
+ }
0 commit comments