| 1 | macro_rules! trace { |
| 2 | ( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => { |
| 3 | if $TRACE { |
| 4 | eprintln!($fmt, $($pargs),*); |
| 5 | } |
| 6 | }; |
| 7 | ( $TRACE:expr, $fmt:expr ) => { |
| 8 | trace!($TRACE, $fmt, ); |
| 9 | }; |
| 10 | } |
| 11 | |
| 12 | // Converts an indentation level to whitespace. |
| 13 | pub(crate) fn indent(i: isize) -> &'static str { |
| 14 | let s = " "; |
| 15 | &s[0..usize::try_from(i).unwrap_or(0).min(s.len())] |
| 16 | } |
| 17 | |
| 18 | macro_rules! tracer { |
| 19 | ( $TRACE:expr, $func:expr ) => { |
| 20 | tracer!($TRACE, $func, 0) |
| 21 | }; |
| 22 | ( $TRACE:expr, $func:expr, $indent:expr ) => { |
| 23 | // Currently, Rust doesn't support $( ... ) in a nested |
| 24 | // macro's definition. See: |
| 25 | // https://users.rust-lang.org/t/nested-macros-issue/8348/2 |
| 26 | macro_rules! t { |
| 27 | ( $fmt:expr ) => |
| 28 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, $fmt) }; |
| 29 | ( $fmt:expr, $a:expr ) => |
| 30 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a)) }; |
| 31 | ( $fmt:expr, $a:expr, $b:expr ) => |
| 32 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b)) }; |
| 33 | ( $fmt:expr, $a:expr, $b:expr, $c:expr ) => |
| 34 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c)) }; |
| 35 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr ) => |
| 36 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d)) }; |
| 37 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr ) => |
| 38 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e)) }; |
| 39 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr ) => |
| 40 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f)) }; |
| 41 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr ) => |
| 42 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g)) }; |
| 43 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr ) => |
| 44 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h)) }; |
| 45 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr ) => |
| 46 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i)) }; |
| 47 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr ) => |
| 48 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j)) }; |
| 49 | ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr ) => |
| 50 | { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)) }; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// A simple shortcut for ensuring a type is send and sync. |
| 56 | /// |
| 57 | /// For most types just call it after defining the type: |
| 58 | /// |
| 59 | /// ```ignore |
| 60 | /// pub struct MyStruct {} |
| 61 | /// assert_send_and_sync!(MyStruct); |
| 62 | /// ``` |
| 63 | /// |
| 64 | /// For types with lifetimes, use the anonymous lifetime: |
| 65 | /// |
| 66 | /// ```ignore |
| 67 | /// pub struct WithLifetime<'a> { _p: std::marker::PhantomData<&'a ()> } |
| 68 | /// assert_send_and_sync!(WithLifetime<'_>); |
| 69 | /// ``` |
| 70 | /// |
| 71 | /// For a type generic over another type `W`, |
| 72 | /// pass the type `W` as a where clause |
| 73 | /// including a trait bound when needed: |
| 74 | /// |
| 75 | /// ```ignore |
| 76 | /// pub struct MyWriter<W: std::io::Write> { _p: std::marker::PhantomData<W> } |
| 77 | /// assert_send_and_sync!(MyWriter<W> where W: std::io::Write); |
| 78 | /// ``` |
| 79 | /// |
| 80 | /// This will assert that `MyWriterStruct<W>` is `Send` and `Sync` |
| 81 | /// if `W` is `Send` and `Sync`. |
| 82 | /// |
| 83 | /// You can also combine the two and be generic over multiple types. |
| 84 | /// Just make sure to list all the types - even those without additional |
| 85 | /// trait bounds: |
| 86 | /// |
| 87 | /// ```ignore |
| 88 | /// pub struct MyWriterWithLifetime<'a, C, W: std::io::Write> { |
| 89 | /// _p: std::marker::PhantomData<&'a (C, W)>, |
| 90 | /// } |
| 91 | /// assert_send_and_sync!(MyWriterWithLifetime<'_, C, W> where C, W: std::io::Write); |
| 92 | /// ``` |
| 93 | /// |
| 94 | /// If you need multiple additional trait bounds on a single type |
| 95 | /// you can add them separated by `+` like in normal where clauses. |
| 96 | /// However you have to make sure they are `Identifiers` like `Write`. |
| 97 | /// In macro patterns `Paths` (like `std::io::Write`) may not be followed |
| 98 | /// by `+` characters. |
| 99 | // Note: We cannot test the macro in doctests, because the macro is |
| 100 | // not public. We test the cases in the test module below, instead. |
| 101 | // If you change the examples here, propagate the changes to the |
| 102 | // module below. |
| 103 | macro_rules! assert_send_and_sync { |
| 104 | ( $x:ty where $( $g:ident$( : $a:path )? $(,)?)*) => { |
| 105 | impl<$( $g ),*> crate::macros::Sendable for $x |
| 106 | where $( $g: Send + Sync $( + $a )? ),* |
| 107 | {} |
| 108 | impl<$( $g ),*> crate::macros::Syncable for $x |
| 109 | where $( $g: Send + Sync $( + $a )? ),* |
| 110 | {} |
| 111 | }; |
| 112 | ( $x:ty where $( $g:ident$( : $a:ident $( + $b:ident )* )? $(,)?)*) => { |
| 113 | impl<$( $g ),*> crate::macros::Sendable for $x |
| 114 | where $( $g: Send + Sync $( + $a $( + $b )* )? ),* |
| 115 | {} |
| 116 | impl<$( $g ),*> crate::macros::Syncable for $x |
| 117 | where $( $g: Send + Sync $( + $a $( + $b )* )? ),* |
| 118 | {} |
| 119 | }; |
| 120 | ( $x:ty ) => { |
| 121 | impl crate::macros::Sendable for $x {} |
| 122 | impl crate::macros::Syncable for $x {} |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | pub(crate) trait Sendable : Send {} |
| 127 | pub(crate) trait Syncable : Sync {} |
| 128 | |
| 129 | /// We cannot test the macro in doctests, because the macro is not |
| 130 | /// public. We test the cases here, instead. If you change the |
| 131 | /// examples here, propagate the changes to the docstring above. |
| 132 | #[cfg(test)] |
| 133 | #[allow(dead_code)] |
| 134 | mod test { |
| 135 | /// For most types just call it after defining the type: |
| 136 | pub struct MyStruct {} |
| 137 | assert_send_and_sync!(MyStruct); |
| 138 | |
| 139 | /// For types with lifetimes, use the anonymous lifetime: |
| 140 | pub struct WithLifetime<'a> { _p: std::marker::PhantomData<&'a ()> } |
| 141 | assert_send_and_sync!(WithLifetime<'_>); |
| 142 | |
| 143 | /// For a type generic over another type `W`, pass the type `W` as |
| 144 | /// a where clause including a trait bound when needed: |
| 145 | pub struct MyWriter<W: std::io::Write> { _p: std::marker::PhantomData<W> } |
| 146 | assert_send_and_sync!(MyWriter<W> where W: std::io::Write); |
| 147 | |
| 148 | /// This will assert that `MyWriterStruct<W>` is `Send` and `Sync` |
| 149 | /// if `W` is `Send` and `Sync`. |
| 150 | /// |
| 151 | /// You can also combine the two and be generic over multiple |
| 152 | /// types. Just make sure to list all the types - even those |
| 153 | /// without additional trait bounds: |
| 154 | pub struct MyWriterWithLifetime<'a, C, W: std::io::Write> { |
| 155 | _p: std::marker::PhantomData<&'a (C, W)>, |
| 156 | } |
| 157 | assert_send_and_sync!(MyWriterWithLifetime<'_, C, W> where C, W: std::io::Write); |
| 158 | } |