Add extension traits for handling Result<Option<T>, E> conversion
This extension traits help transforming Result<Option<T>, E> to Result<T, E> or transforming an iterator over the former type to an iterator over the latter type. Should be moved to resiter of course, but we need to implement this first. Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
e7e5d30645
commit
0e20b25091
3 changed files with 85 additions and 0 deletions
|
@ -128,3 +128,49 @@ impl<I, T> TraceIterator<T> for I where
|
||||||
I: Iterator<Item = Result<T, Error>>
|
I: Iterator<Item = Result<T, Error>>
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
/// Extension trait for doing
|
||||||
|
///
|
||||||
|
/// ```ignore
|
||||||
|
/// Iterator<Item = Result<Option<T>, E>> -> Iterator<Item = Result<T, E>>
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
pub trait IterInnerOkOrElse<T, E, F>
|
||||||
|
where T: Sized,
|
||||||
|
E: Sized,
|
||||||
|
Self: Iterator<Item = Result<Option<T>, E>> + Sized,
|
||||||
|
F: Fn() -> E,
|
||||||
|
{
|
||||||
|
fn map_inner_ok_or_else(self, f: F) -> IterInnerOkOrElseImpl<Self, T, E, F>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IterInnerOkOrElseImpl<I, T, E, F>(I, F)
|
||||||
|
where I: Iterator<Item = Result<Option<T>, E>> + Sized,
|
||||||
|
T: Sized,
|
||||||
|
E: Sized,
|
||||||
|
F: Fn() -> E;
|
||||||
|
|
||||||
|
impl<I, T, E, F> IterInnerOkOrElse<T, E, F> for I
|
||||||
|
where I: Iterator<Item = Result<Option<T>, E>> + Sized,
|
||||||
|
T: Sized,
|
||||||
|
E: Sized,
|
||||||
|
F: Fn() -> E,
|
||||||
|
{
|
||||||
|
fn map_inner_ok_or_else(self, f: F) -> IterInnerOkOrElseImpl<I, T, E, F> {
|
||||||
|
IterInnerOkOrElseImpl(self, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I, T, E, F> Iterator for IterInnerOkOrElseImpl<I, T, E, F>
|
||||||
|
where I: Iterator<Item = Result<Option<T>, E>> + Sized,
|
||||||
|
T: Sized,
|
||||||
|
E: Sized,
|
||||||
|
F: Fn() -> E,
|
||||||
|
{
|
||||||
|
type Item = Result<T, E>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.0.next().map(|e| e.and_then(|opt| opt.ok_or_else(|| (self.1)())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ pub mod errors;
|
||||||
pub mod exit;
|
pub mod exit;
|
||||||
pub mod io;
|
pub mod io;
|
||||||
pub mod iter;
|
pub mod iter;
|
||||||
|
pub mod result;
|
||||||
pub mod str;
|
pub mod str;
|
||||||
pub mod trace;
|
pub mod trace;
|
||||||
|
|
||||||
|
|
38
lib/core/libimagerror/src/result.rs
Normal file
38
lib/core/libimagerror/src/result.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
//
|
||||||
|
// imag - the personal information management suite for the commandline
|
||||||
|
// Copyright (C) 2015-2019 the imag contributors
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; version
|
||||||
|
// 2.1 of the License.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
//
|
||||||
|
|
||||||
|
/// Extension trait for doing `Result<Option<T>, E> -> Result<T, E>`
|
||||||
|
pub trait ResultOptionExt<T, E, F>
|
||||||
|
where T: Sized,
|
||||||
|
E: Sized,
|
||||||
|
F: FnOnce() -> E
|
||||||
|
{
|
||||||
|
fn inner_ok_or_else(self, f: F) -> Result<T, E>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E, F> ResultOptionExt<T, E, F> for Result<Option<T>, E>
|
||||||
|
where T: Sized,
|
||||||
|
E: Sized,
|
||||||
|
F: FnOnce() -> E
|
||||||
|
{
|
||||||
|
fn inner_ok_or_else(self, f: F) -> Result<T, E> {
|
||||||
|
self.and_then(|opt| opt.ok_or_else(f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue