-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from triarius/fix
Fix blocking on stdin
- Loading branch information
Showing
4 changed files
with
194 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,50 @@ | ||
/// A trait for `Result` that allows chaining a function that returns a `Result` to the error type. | ||
/// | ||
/// # Examples | ||
/// One use case is if the ok type, `T` has a default value, and the error type, `E` may take | ||
/// multiple values that may be ignored, then this trait can be used to map the error type to the | ||
/// default value of `T` for the errors that can be ignored. | ||
pub(crate) trait FlatMapErr<T, D, E> { | ||
fn flat_map_err<F>(self, f: F) -> Result<T, E> | ||
where | ||
F: FnOnce(D) -> Result<T, E>; | ||
} | ||
|
||
impl<T, D, E> FlatMapErr<T, D, E> for Result<T, D> { | ||
fn flat_map_err<F>(self, f: F) -> Result<T, E> | ||
where | ||
F: FnOnce(D) -> Result<T, E>, | ||
{ | ||
match self.map_err(f) { | ||
Ok(t) | Err(Ok(t)) => Ok(t), | ||
Err(Err(e)) => Err(e), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
#[test] | ||
fn flat_map_err() { | ||
use super::FlatMapErr; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
enum Error { | ||
IgnorableError, | ||
GraveError, | ||
} | ||
|
||
let ignore = |e| match e { | ||
Error::IgnorableError => Ok(()), | ||
Error::GraveError => Err(e), | ||
}; | ||
|
||
let success = Ok(()); | ||
let ignorable_error = Err(Error::IgnorableError); | ||
let grave_error = Err(Error::GraveError); | ||
|
||
assert_eq!(success.flat_map_err(ignore), Ok(())); | ||
assert_eq!(ignorable_error.flat_map_err(ignore), Ok(())); | ||
assert_eq!(grave_error.flat_map_err(ignore), Err(Error::GraveError)); | ||
} | ||
} |
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