-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #898
- Loading branch information
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use futures_core::{Future, Poll, Async}; | ||
use futures_core::task; | ||
|
||
/// Do something with the error of a future, passing it on. | ||
/// | ||
/// This is created by the [`FutureExt::inspect_err`] method. | ||
#[derive(Debug)] | ||
#[must_use = "futures do nothing unless polled"] | ||
pub struct InspectErr<A, F> where A: Future { | ||
future: A, | ||
f: Option<F>, | ||
} | ||
|
||
pub fn new<A, F>(future: A, f: F) -> InspectErr<A, F> | ||
where A: Future, | ||
F: FnOnce(&A::Error), | ||
{ | ||
InspectErr { | ||
future: future, | ||
f: Some(f), | ||
} | ||
} | ||
|
||
impl<A, F> Future for InspectErr<A, F> | ||
where A: Future, | ||
F: FnOnce(&A::Error), | ||
{ | ||
type Item = A::Item; | ||
type Error = A::Error; | ||
|
||
fn poll(&mut self, cx: &mut task::Context) -> Poll<A::Item, A::Error> { | ||
match self.future.poll(cx) { | ||
Ok(Async::Pending) => Ok(Async::Pending), | ||
Ok(Async::Ready(e)) => Ok(Async::Ready(e)), | ||
Err(e) => { | ||
(self.f.take().expect("cannot poll InspectErr twice"))(&e); | ||
Err(e) | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters