maybe_fut/context.rs
1/// Returns whether the current code is being executed in an async context.
2///
3/// If tokio is disabled, this function will always return false.
4#[inline]
5pub fn is_async_context() -> bool {
6 #[cfg(tokio)]
7 {
8 tokio::runtime::Handle::try_current().is_ok()
9 }
10 #[cfg(not(tokio))]
11 {
12 false
13 }
14}
15
16#[cfg(test)]
17mod test {
18
19 use super::*;
20
21 #[test]
22 fn test_should_return_false_if_not_in_async_context() {
23 assert!(!is_async_context(),);
24 }
25
26 #[tokio::test]
27 async fn test_should_return_true_if_in_async_context() {
28 assert!(is_async_context());
29 }
30}