strided_kernel/
maybe_sync.rs

1//! Feature-gated Send/Sync marker traits.
2//!
3//! When the `parallel` feature is enabled, [`MaybeSend`] ≡ [`Send`],
4//! [`MaybeSync`] ≡ [`Sync`], and [`MaybeSendSync`] ≡ [`Send`] + [`Sync`].
5//!
6//! When `parallel` is disabled, these traits are blanket-implemented
7//! for all types, so non-thread-safe types can use the kernel APIs.
8
9// ---- parallel enabled: alias to real Send/Sync ----
10
11/// Equivalent to [`Send`] when `parallel` is enabled; blanket-impl otherwise.
12#[cfg(feature = "parallel")]
13pub trait MaybeSend: Send {}
14#[cfg(feature = "parallel")]
15impl<T: Send> MaybeSend for T {}
16
17/// Equivalent to [`Sync`] when `parallel` is enabled; blanket-impl otherwise.
18#[cfg(feature = "parallel")]
19pub trait MaybeSync: Sync {}
20#[cfg(feature = "parallel")]
21impl<T: Sync> MaybeSync for T {}
22
23/// Equivalent to [`Send`] + [`Sync`] when `parallel` is enabled; blanket-impl otherwise.
24#[cfg(feature = "parallel")]
25pub trait MaybeSendSync: Send + Sync {}
26#[cfg(feature = "parallel")]
27impl<T: Send + Sync> MaybeSendSync for T {}
28
29// ---- parallel disabled: blanket impl for all types ----
30
31/// Equivalent to [`Send`] when `parallel` is enabled; blanket-impl otherwise.
32#[cfg(not(feature = "parallel"))]
33pub trait MaybeSend {}
34#[cfg(not(feature = "parallel"))]
35impl<T> MaybeSend for T {}
36
37/// Equivalent to [`Sync`] when `parallel` is enabled; blanket-impl otherwise.
38#[cfg(not(feature = "parallel"))]
39pub trait MaybeSync {}
40#[cfg(not(feature = "parallel"))]
41impl<T> MaybeSync for T {}
42
43/// Equivalent to [`Send`] + [`Sync`] when `parallel` is enabled; blanket-impl otherwise.
44#[cfg(not(feature = "parallel"))]
45pub trait MaybeSendSync {}
46#[cfg(not(feature = "parallel"))]
47impl<T> MaybeSendSync for T {}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_f64_satisfies_maybe_traits() {
55        fn _check_send<T: MaybeSend>() {}
56        fn _check_sync<T: MaybeSync>() {}
57        fn _check_send_sync<T: MaybeSendSync>() {}
58        _check_send::<f64>();
59        _check_sync::<f64>();
60        _check_send_sync::<f64>();
61    }
62
63    #[cfg(not(feature = "parallel"))]
64    #[test]
65    fn test_rc_satisfies_maybe_traits_without_parallel() {
66        use std::rc::Rc;
67        fn _check_send<T: MaybeSend>() {}
68        fn _check_sync<T: MaybeSync>() {}
69        fn _check_send_sync<T: MaybeSendSync>() {}
70        _check_send::<Rc<f64>>();
71        _check_sync::<Rc<f64>>();
72        _check_send_sync::<Rc<f64>>();
73    }
74}