trueno/brick/patterns/
async_result.rs1#[derive(Debug, Clone)]
5pub enum AsyncResult<T, E> {
6 Async(T),
8 Sync(T),
10 Error(E),
12}
13
14impl<T, E> AsyncResult<T, E> {
15 #[must_use]
17 pub fn is_async(&self) -> bool {
18 matches!(self, AsyncResult::Async(_))
19 }
20
21 #[must_use]
23 pub fn is_sync(&self) -> bool {
24 matches!(self, AsyncResult::Sync(_))
25 }
26
27 #[must_use]
29 pub fn is_error(&self) -> bool {
30 matches!(self, AsyncResult::Error(_))
31 }
32
33 pub fn into_result(self) -> Result<T, E> {
35 match self {
36 AsyncResult::Async(v) | AsyncResult::Sync(v) => Ok(v),
37 AsyncResult::Error(e) => Err(e),
38 }
39 }
40
41 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> AsyncResult<U, E> {
43 match self {
44 AsyncResult::Async(v) => AsyncResult::Async(f(v)),
45 AsyncResult::Sync(v) => AsyncResult::Sync(f(v)),
46 AsyncResult::Error(e) => AsyncResult::Error(e),
47 }
48 }
49}