1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pub use af_core_macros::{future_boxed as boxed, future_boxed_local as boxed_local};
pub use std::future::Future;
pub use std::task::{Context, Poll};
mod noop_waker;
mod try_future;
pub use self::try_future::*;
use crate::prelude::*;
pub async fn catch_unwind<F>(f: F) -> Result<F::Output, Box<dyn Any + Send>>
where
F: Future + panic::UnwindSafe,
{
use futures_lite::FutureExt;
f.catch_unwind().await
}
pub async fn forever<T>() -> T {
futures_lite::future::pending().await
}
pub fn poll<F: Future + Unpin>(f: &mut F) -> Option<F::Output> {
match Pin::new(f).poll(&mut noop_waker::context()) {
Poll::Ready(value) => Some(value),
_ => None,
}
}
pub async fn race<T>(a: impl Future<Output = T>, b: impl Future<Output = T>) -> T {
use futures_lite::FutureExt;
a.or(b).await
}
pub fn try_resolve<T>(f: impl Future<Output = T>) -> Option<T> {
pin!(f);
poll(&mut f)
}