Skip to main content

tuwunel_core/utils/
option.rs

1use futures::{FutureExt, Stream, future::OptionFuture};
2
3use super::IterStream;
4
5pub trait OptionExt<T> {
6	fn map_async<F, Fut, U>(self, f: F) -> OptionFuture<Fut>
7	where
8		F: FnOnce(T) -> Fut,
9		Fut: Future<Output = U> + Send,
10		U: Send;
11
12	#[inline]
13	fn map_stream<F, Fut, U>(self, f: F) -> impl Stream<Item = U> + Send
14	where
15		F: FnOnce(T) -> Fut,
16		Fut: Future<Output = U> + Send,
17		U: Send,
18		Self: Sized,
19	{
20		self.map_async(f)
21			.map(Option::into_iter)
22			.map(IterStream::stream)
23			.flatten_stream()
24	}
25}
26
27impl<T> OptionExt<T> for Option<T> {
28	#[inline]
29	fn map_async<F, Fut, U>(self, f: F) -> OptionFuture<Fut>
30	where
31		F: FnOnce(T) -> Fut,
32		Fut: Future<Output = U> + Send,
33		U: Send,
34	{
35		self.map(f).into()
36	}
37}