Skip to content

Commit 4430be1

Browse files
committed
test
1 parent 0896eeb commit 4430be1

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ edition = "2018"
1212

1313
[dependencies]
1414
pin-utils = "=0.1.0-alpha.4"
15+
async-trait = "0.1"
1516

1617
[dependencies.futures]
1718
version = "=0.3.0-alpha.17"

examples/future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
executor::block_on(async {
88
let future = ready(Ok::<i32, i32>(1));
99
let future = and_then(future, |x| ready(Ok::<i32, i32>(x + 3)));
10-
let future = inspect(future, |x| { dbg!(x); });
10+
let future = future.inspect(|x| { dbg!(x); });
1111
assert_eq!(future.await, Ok(4));
1212
});
1313
}

src/future.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
1-
use futures::future::Future;
1+
use core::future::Future;
22
use futures::stream::Stream;
3-
43
use core::task::{Poll, Context};
4+
use async_trait::async_trait;
55

66
pub async fn ready<T>(value: T) -> T {
77
value
88
}
99

10-
pub async fn map<Fut, U, F>(future: Fut, f: F) -> U
11-
where F: FnOnce(Fut::Output) -> U,
12-
Fut: Future,
13-
{
14-
f(future.await)
15-
}
10+
impl<T: ?Sized> FutureExt for T where T: Future {}
1611

17-
pub async fn then<FutA, FutB, F>(future: FutA, f: F) -> FutB::Output
18-
where F: FnOnce(FutA::Output) -> FutB,
19-
FutA: Future,
20-
FutB: Future,
21-
{
22-
let new_future = f(future.await);
23-
new_future.await
12+
#[async_trait]
13+
pub trait FutureExt: Future {
14+
async fn map<U, F>(self, f: F) -> U
15+
where F: FnOnce(Self::Output) -> U + Send,
16+
Self: Sized,
17+
{
18+
f(self.await)
19+
}
20+
21+
async fn then<Fut, F>(self, f: F) -> Fut::Output
22+
where F: FnOnce(Self::Output) -> Fut + Send,
23+
Fut: Future + Send,
24+
Self: Sized,
25+
{
26+
let new_future = f(self.await);
27+
new_future.await
28+
}
29+
30+
async fn flatten(self) -> <<Self as Future>::Output as Future>::Output
31+
where <Self as Future>::Output: Future + Send,
32+
Self: Sized,
33+
{
34+
let nested_future = self.await;
35+
nested_future.await
36+
}
37+
38+
async fn inspect<F>(self, f: F) -> Self::Output
39+
where F: FnOnce(&Self::Output) + Send,
40+
Self: Sized,
41+
{
42+
let future_result = self.await;
43+
f(&future_result);
44+
future_result
45+
}
2446
}
2547

48+
49+
2650
pub async fn and_then<FutA, FutB, F, T, U, E>(future: FutA, f: F) -> Result<U, E>
2751
where F: FnOnce(T) -> FutB,
2852
FutA: Future<Output = Result<T,E>>,
@@ -65,23 +89,6 @@ pub async fn map_err<Fut, F, T, E, U>(future: Fut, f: F) -> Result<T, U>
6589
future.await.map_err(f)
6690
}
6791

68-
pub async fn flatten<FutA, FutB>(future: FutA) -> FutB::Output
69-
where FutA: Future<Output = FutB>,
70-
FutB: Future,
71-
{
72-
let nested_future = future.await;
73-
nested_future.await
74-
}
75-
76-
pub async fn inspect<Fut, F>(future: Fut, f: F) -> Fut::Output
77-
where Fut: Future,
78-
F: FnOnce(&Fut::Output),
79-
{
80-
let future_result = future.await;
81-
f(&future_result);
82-
future_result
83-
}
84-
8592
pub async fn err_into<Fut, T, E, U>(future: Fut) -> Result<T,U>
8693
where Fut: Future<Output = Result<T,E>>,
8794
E: Into<U>,
@@ -165,7 +172,7 @@ mod tests {
165172
fn test_map() {
166173
executor::block_on(async {
167174
let future = ready(1);
168-
let new_future = map(future, |x| x + 3);
175+
let new_future = future.map(|x| x + 3);
169176
assert_eq!(new_future.await, 4);
170177
});
171178
}
@@ -174,7 +181,7 @@ mod tests {
174181
fn test_then() {
175182
executor::block_on(async {
176183
let future = ready(1);
177-
let new_future = then(future, |x| ready(x + 3));
184+
let new_future = future.then(|x| ready(x + 3));
178185
assert_eq!(new_future.await, 4);
179186
});
180187
}
@@ -228,7 +235,7 @@ mod tests {
228235
fn test_flatten() {
229236
executor::block_on(async {
230237
let nested_future = ready(ready(1));
231-
let future = flatten(nested_future);
238+
let future = nested_future.flatten();
232239
assert_eq!(future.await, 1);
233240
});
234241
}
@@ -237,7 +244,7 @@ mod tests {
237244
fn test_inspect() {
238245
executor::block_on(async {
239246
let future = ready(1);
240-
let new_future = inspect(future, |&x| assert_eq!(x, 1));
247+
let new_future = future.inspect(|&x| assert_eq!(x, 1));
241248
assert_eq!(new_future.await, 1);
242249
});
243250
}

0 commit comments

Comments
 (0)