Browse Source

documented few functions

pull/1/head
unknown 10 months ago
parent
commit
b15e74f5c5
  1. 60
      src/lib.rs

60
src/lib.rs

@ -148,6 +148,9 @@ where
todo!("hmmm");
}
/// Maps an `EResult<O, I, E>` to `EResult<U, I, E>` by applying a function to a contained [`Ok`]
/// value, leaving both the `InErr` and `ExtErr` untouched. This function can be used to compose the
/// results of two functions.
pub fn map<U, F>(self, f: F) -> EResult<U, I, E>
where
F: FnOnce(O) -> U,
@ -159,6 +162,10 @@ where
}
}
/// Applies a function to the contained value (if [`Ok']), or returns the provided default (if
/// either `InErr` or `ExtErr`). Arguments passed to `map_or` are eagerly evaluated; if you are
/// passing the result of a function call, it is recommended to use [`map_or_else`], which is
/// lazily evaluated.
pub fn map_or<U, F>(self, default: U, f: F) -> U
where
F: FnOnce(O) -> U,
@ -170,6 +177,9 @@ where
}
}
/// Maps a `EResult<O, I, E> to `U` by applying a function to a contained `Ok` value, or a
/// fallback function to a contained `InErr` or `ExtErr` value. This function can be used to
/// unpack a successful result while handling an error.
pub fn map_or_else<U, ID, ED, F>(self, in_default: ID, ext_default: ED, f: F) -> U
where
F: FnOnce(O) -> U,
@ -183,6 +193,9 @@ where
}
}
/// Maps an `EResult<O, I, E>` to `EResult<O, U, E> by applying a function to a contained `InErr`
/// value, leaving `Ok` value untouched. This function can be used to pass through a
/// successful result while handling an error.
pub fn map_in_err<U, F>(self, op: F) -> EResult<O, U, E>
where
U: Debug + InErr,
@ -199,6 +212,9 @@ where
}
}
/// Maps an `EResult<O, I, E>` to `EResult<O, I, U> by applying a function to a contained `ExtErr`
/// value, leaving `Ok` value untouched. This function can be used to pass through a
/// successful result while handling an error.
pub fn map_ext_err<U, F>(self, op: F) -> EResult<O, I, U>
where
U: Debug + ExtErr,
@ -211,6 +227,9 @@ where
}
}
/// Maps an `EResult<O, I, E> to `EResult<O, U, V> by applying functions `F` and `G` to a
/// contained `InErr` and `ExtErr` respectively, leaving an `Ok` value untouched. This function
/// can be used to pass through a successful result while handling an error.
pub fn map_err<U, V, F, G>(self, op_in: F, op_ext: G) -> EResult<O, U, V>
where
U: Debug + InErr,
@ -229,6 +248,8 @@ where
}
}
/// Returns an iterator over the possibly contained value.
/// The iterator yields one value if the result is [`EResult::Ok`], otherwise none.
pub fn iter<'a>(&'a self) -> Iter<'a, O>
where
&'a I: InErr + Debug,
@ -237,6 +258,8 @@ where
Iter { inner: self.as_ref().ok() }
}
/// Returns a mutable iterator over the possibly contained value.
/// The iterator yields one value if the result is [`EResult::Ok`], otherwise none.
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, O>
where
&'a mut I: InErr + Debug,
@ -245,6 +268,8 @@ where
IterMut { inner: self.as_mut().ok() }
}
/// Returns `res` if the result is `Ok`, otherwise returns the `InErr` and `ExtErr` value of
/// `self`.
pub fn and<U>(self, res: EResult<U, I, E>) -> EResult<U, I, E> {
match self {
Self::Ok(_) => res,
@ -253,6 +278,8 @@ where
}
}
/// Calls `op` if the result is `Ok`, otherwise returns the `InErr` and `ExtErr` value of `self`.
/// This function can be used for control flow based on `EResult` values.
pub fn and_then<U, F>(self, op: F) -> EResult<U, I, E>
where
F: FnOnce(O) -> EResult<U, I, E>,
@ -264,6 +291,9 @@ where
}
}
/// Returns `res` if the result is `InErr` or `ExtIerr`, otherwise returns the `Ok` value of
/// `self`. Arguments passed to `or` are eagerly evaluated; if you are passing the result of a
/// function call, it is recommended to use [`or_else`], which is lazily evaluated.
pub fn or<F, G>(self, res: EResult<O, F, G>) -> EResult<O, F, G>
where
F: InErr + Debug,
@ -276,6 +306,8 @@ where
}
}
/// Calls `op` if the result is `InErr` or `ExtErr`, otherwise returns the `Ok` value of
/// `self`. This function can be used for control flow based on result values.
pub fn or_else<F, G, H>(self, op: H) -> EResult<O, F, G> // TODO does this even make sense?
where
F: InErr + Debug,
@ -289,6 +321,9 @@ where
}
}
/// Returns the contained `Ok` value or a provided default. Arguments passed to `unwrap_or` are
/// eagerly evaluated; if you are passing the result of a function call, it is recommended to
/// use [`unwrap_or_else`], which is lazily evaluated.
pub fn unwrap_or(self, default: O) -> O {
match self {
Self::Ok(o) => o,
@ -296,6 +331,7 @@ where
}
}
/// Returns the contained `Ok` value or computes it from a closure.
pub fn unwrap_or_else<F>(self, op: F) -> O
where
F: FnOnce(Error<I, E>) -> O,
@ -310,6 +346,11 @@ where
// copy
// clone
/// Returns the contained `Ok` value, consuming the `self` value.
///
/// # Panics
/// Panics if the value is an `InErr` or `ExtErr`, with a panic message including the passed
/// message, and the content of the `InErr` or `ExtErr`.
pub fn expect(self, msg: &str) -> O {
match self {
Self::Ok(o) => o,
@ -318,6 +359,13 @@ where
}
}
/// Returns the contained `Ok` value, consuming the `self` value. Because this function may
/// panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle
/// the `Err` cases explicitly, or call `unwrap_or`, `unwrap_or_else`, or `unwrap_or_default`.
///
/// # Panics
/// Panics if the value is an `InErr` or `ExtErr`, with a panic message provided by the `Err`
/// variants value.
pub fn unwrap(self) -> O {
match self {
Self::Ok(o) => o,
@ -404,6 +452,8 @@ where
I: InErr + Debug
{
#[cfg(feature = "result_copied")]
/// Maps an `EResult<&O, I, E> to an `EResult<O, I, E>` by copying the contents of the `Ok`
/// part.
pub fn copied(self) -> EResult<O, I, E> {
match self {
Self::Ok(o) => EResult::Ok(*o),
@ -419,6 +469,8 @@ where
I: InErr + Debug
{
#[cfg(feature = "result_copied")]
/// Maps an `EResult<&mut O, I, E> to an `EResult<O, I, E>` by copying the contents of the `Ok`
/// part.
pub fn copied(self) -> EResult<O, I, E> {
match self {
Self::Ok(o) => EResult::Ok(*o),
@ -434,6 +486,8 @@ where
I: InErr + Debug
{
#[cfg(feature = "result_cloned")]
/// Maps an `EResult<&O, I, E>` to an `EResult<O, I, E>` by cloning the contents of the `Ok`
/// part.
pub fn copied(self) -> EResult<O, I, E> {
match self {
Self::Ok(o) => EResult::Ok(o.clone()),
@ -449,6 +503,8 @@ where
I: InErr + Debug
{
#[cfg(feature = "result_cloned")]
/// Maps an `EResult<&mut O, I, E>` to an `EResult<O, I, E>` by cloning the contents of the `Ok`
/// part.
pub fn copied(self) -> EResult<O, I, E> {
match self {
Self::Ok(o) => EResult::Ok(o.clone()),
@ -463,6 +519,9 @@ where
E: ExtErr + Debug,
I: InErr + Debug
{
/// Transpose an `EResult` of an `Option` into an `Option` of an `EResult`. `Ok(None) will be
/// mapped to `None`. `Ok(Some(_)` will be mapped to `Some(Ok(_))` and `InErr(_)` and
/// `ExtErr(_)` will be mapped to `Some(InErr(_))` and `Some(ExtErr(_))`.
pub fn transpose(self) -> Option<EResult<O, I, E>> {
match self {
Self::Ok(Some(o)) => Some(EResult::Ok(o)),
@ -478,6 +537,7 @@ where
E: ExtErr + Debug,
I: InErr + Debug
{
/// Converts from `EResult<EResult<O, I, E>, I, E>` to `EResult<O, I, E>`.
pub fn flatten(self) -> EResult<O, I, E> {
match self {
Self::Ok(o) => match o {

Loading…
Cancel
Save