fusio_core/maybe.rs
1use core::future::Future;
2
3/// A trait representing types that may or may not require [`Send`].
4///
5/// Many async runtimes do not require [`Send`] for futures and streams. This trait
6/// provides a way to represent types that may not require [`Send`]. Users can
7/// switch the feature `no-send` at compile-time to disable the [`Send`] bound.
8///
9/// # Safety
10///
11/// Do not implement this trait directly. It is automatically implemented for all
12/// types based on the feature flags.
13#[cfg(not(feature = "no-send"))]
14pub unsafe trait MaybeSend: Send {}
15
16/// A trait representing types that may or may not require [`Send`].
17///
18/// When the `no-send` feature is enabled, this trait has no `Send` bound,
19/// allowing use with single-threaded async runtimes.
20///
21/// # Safety
22///
23/// Do not implement this trait directly. It is automatically implemented for all types.
24#[cfg(feature = "no-send")]
25pub unsafe trait MaybeSend {}
26
27#[cfg(not(feature = "no-send"))]
28unsafe impl<T: Send> MaybeSend for T {}
29#[cfg(feature = "no-send")]
30unsafe impl<T> MaybeSend for T {}
31
32/// A trait representing types that may or may not require [`Sync`].
33///
34/// Same as [`MaybeSend`], but for [`Sync`]. Users can switch the feature `no-send`
35/// at compile-time to disable the [`Sync`] bound.
36///
37/// # Safety
38///
39/// Do not implement this trait directly. It is automatically implemented for all
40/// types based on the feature flags.
41#[cfg(not(feature = "no-send"))]
42pub unsafe trait MaybeSync: Sync {}
43
44/// A trait representing types that may or may not require [`Sync`].
45///
46/// When the `no-send` feature is enabled, this trait has no `Sync` bound,
47/// allowing use with single-threaded async runtimes.
48///
49/// # Safety
50///
51/// Do not implement this trait directly. It is automatically implemented for all types.
52#[cfg(feature = "no-send")]
53pub unsafe trait MaybeSync {}
54
55#[cfg(not(feature = "no-send"))]
56unsafe impl<T: Sync> MaybeSync for T {}
57#[cfg(feature = "no-send")]
58unsafe impl<T> MaybeSync for T {}
59
60/// A trait for determining whether the buffer is owned or borrowed.
61///
62/// Poll-based I/O operations require the buffer to be borrowed, while completion-based I/O
63/// operations require the buffer to be owned. This trait provides a way to abstract over
64/// the ownership of the buffer. Users can switch between poll-based and completion-based
65/// I/O operations at compile-time by enabling or disabling the `completion-based` feature.
66///
67/// # Safety
68///
69/// Do not implement this trait manually. It is automatically implemented based on
70/// the feature flags.
71#[cfg(not(feature = "completion-based"))]
72pub unsafe trait MaybeOwned {}
73#[cfg(not(feature = "completion-based"))]
74unsafe impl<T> MaybeOwned for T {}
75
76/// A trait for determining whether the buffer is owned or borrowed.
77///
78/// When the `completion-based` feature is enabled, this trait requires a `'static`
79/// lifetime bound to ensure buffers can be safely used in completion-based I/O.
80///
81/// # Safety
82///
83/// Do not implement this trait manually. It is automatically implemented for all
84/// types with a `'static` lifetime.
85#[cfg(feature = "completion-based")]
86pub unsafe trait MaybeOwned: 'static {}
87
88#[cfg(feature = "completion-based")]
89unsafe impl<T: 'static> MaybeOwned for T {}
90
91/// A trait representing futures that may or may not require [`Send`].
92///
93/// This trait combines [`Future`] with [`MaybeSend`], providing a convenient
94/// bound for async functions that may or may not require `Send` based on
95/// the feature flags.
96pub trait MaybeSendFuture: Future + MaybeSend {}
97
98impl<F> MaybeSendFuture for F where F: Future + MaybeSend {}