Skip to content

Commit 5e054cb

Browse files
committed
use \'asyc_trait in TryFutureExt
1 parent 1191ca4 commit 5e054cb

File tree

1 file changed

+30
-54
lines changed

1 file changed

+30
-54
lines changed

src/future.rs

+30-54
Original file line numberDiff line numberDiff line change
@@ -45,51 +45,17 @@ pub trait FutureExt: Future {
4545
}
4646
}
4747

48-
#[async_trait]
49-
pub trait TryFuture: Future {
50-
type Ok;
51-
type Error;
52-
53-
async fn and_then<U, F, FutB>(self, f: F) -> Result<U, Self::Error>
54-
where F: FnOnce(Self::Ok) -> FutB + Send,
55-
FutB: Future<Output = Result<U, Self::Error>> + Send,
56-
Self: Sized;
57-
58-
async fn or_else<U, F, FutB>(self, f: F) -> Result<Self::Ok, U>
59-
where F: FnOnce(Self::Error) -> FutB + Send,
60-
FutB: Future<Output = Result<Self::Ok, U>> + Send,
61-
Self: Sized;
62-
63-
async fn map_ok<U, F>(self, f: F) -> Result<U, Self::Error>
64-
where F: FnOnce(Self::Ok) -> U + Send,
65-
Self: Sized;
66-
67-
async fn map_err<U, F>(self, f: F) -> Result<Self::Ok, U>
68-
where F: FnOnce(Self::Error) -> U + Send,
69-
Self: Sized;
70-
71-
async fn err_into<U>(self) -> Result<Self::Ok, U>
72-
where Self::Error: Into<U>,
73-
Self: Sized;
74-
75-
async fn unwrap_or_else<F>(self, f: F) -> Self::Ok
76-
where F: FnOnce(Self::Error) -> Self::Ok + Send,
77-
Self: Sized;
78-
}
48+
impl<T, E, Fut: ?Sized> TryFutureExt<T, E> for Fut where Fut: Future<Output = Result<T, E>> {}
7949

8050
#[async_trait]
81-
impl<T, E, Fut> TryFuture for Fut
82-
where Fut: ?Sized + Future<Output = Result<T, E>> + Send,
83-
T: Send + 'static,
84-
E: Send + 'static,
85-
{
86-
type Ok = T;
87-
type Error = E;
51+
pub trait TryFutureExt<T, E>: Future<Output = Result<T, E>> {
8852

89-
async fn and_then<U, F, FutB>(self, f: F) -> Result<U, Self::Error>
90-
where F: FnOnce(Self::Ok) -> FutB + Send,
91-
FutB: Future<Output = Result<U, Self::Error>> + Send,
92-
Self: Sized
53+
async fn and_then<U, F, FutB>(self, f: F) -> Result<U, E>
54+
where F: FnOnce(T) -> FutB + Send,
55+
FutB: Future<Output = Result<U, E>> + Send,
56+
Self: Sized,
57+
T: Send + 'async_trait,
58+
E: Send + 'async_trait,
9359
{
9460
match self.await {
9561
Ok(ok) => {
@@ -100,10 +66,12 @@ impl<T, E, Fut> TryFuture for Fut
10066
}
10167
}
10268

103-
async fn or_else<U, F, FutB>(self, f: F) -> Result<Self::Ok, U>
104-
where F: FnOnce(Self::Error) -> FutB + Send,
105-
FutB: Future<Output = Result<Self::Ok, U>> + Send,
69+
async fn or_else<U, F, FutB>(self, f: F) -> Result<T, U>
70+
where F: FnOnce(E) -> FutB + Send,
71+
FutB: Future<Output = Result<T, U>> + Send,
10672
Self: Sized,
73+
T: Send + 'async_trait,
74+
E: Send + 'async_trait,
10775
{
10876
match self.await {
10977
Ok(ok) => Ok(ok),
@@ -114,30 +82,38 @@ impl<T, E, Fut> TryFuture for Fut
11482
}
11583
}
11684

117-
async fn map_ok<U, F>(self, f: F) -> Result<U, Self::Error>
118-
where F: FnOnce(Self::Ok) -> U + Send,
119-
Self: Sized
85+
async fn map_ok<U, F>(self, f: F) -> Result<U, E>
86+
where F: FnOnce(T) -> U + Send,
87+
Self: Sized,
88+
T: Send + 'async_trait,
89+
E: Send + 'async_trait,
12090
{
12191
self.await.map(f)
12292
}
12393

124-
async fn map_err<U, F>(self, f: F) -> Result<Self::Ok, U>
125-
where F: FnOnce(Self::Error) -> U + Send,
94+
async fn map_err<U, F>(self, f: F) -> Result<T, U>
95+
where F: FnOnce(E) -> U + Send,
12696
Self: Sized,
97+
T: Send + 'async_trait,
98+
E: Send + 'async_trait,
12799
{
128100
self.await.map_err(f)
129101
}
130102

131-
async fn err_into<U>(self) -> Result<Self::Ok, U>
132-
where Self::Error: Into<U>,
103+
async fn err_into<U>(self) -> Result<T, U>
104+
where E: Into<U>,
133105
Self: Sized,
106+
T: Send + 'async_trait,
107+
E: Send + 'async_trait,
134108
{
135109
self.await.map_err(Into::into)
136110
}
137111

138-
async fn unwrap_or_else<F>(self, f: F) -> Self::Ok
139-
where F: FnOnce(Self::Error) -> Self::Ok + Send,
112+
async fn unwrap_or_else<F>(self, f: F) -> T
113+
where F: FnOnce(E) -> T + Send,
140114
Self: Sized,
115+
T: Send + 'async_trait,
116+
E: Send + 'async_trait,
141117
{
142118
self.await.unwrap_or_else(f)
143119
}

0 commit comments

Comments
 (0)