rs_std_ext/
unwrap.rs

1//! This module contains a few quick `unwrap`s
2//! for composite `Result`/`Option` types.
3
4use std::fmt::Debug;
5
6/// Extension for `unwrap` methods.
7pub trait UnwrapExt {
8    type Output;
9
10    /// `unwrap` the value.
11    /// 
12    /// ## Example
13    /// 
14    /// ```rust
15    /// use rs_std_ext::unwrap::UnwrapExt;
16    /// 
17    /// let val: Result<Option<i32>, &str> = Ok(Some(10));
18    /// assert_eq!(
19    ///     10,
20    ///     val.unwrap_all()
21    /// )
22    /// ```
23    fn unwrap_all(self) -> Self::Output;
24}
25
26impl<T, E: Debug> UnwrapExt for Result<Option<T>, E> {
27    type Output = T;
28
29    fn unwrap_all(self) -> Self::Output {
30        self.unwrap().unwrap()
31    }
32}
33
34impl<T, E1: Debug, E2: Debug> UnwrapExt for Result<Result<T, E1>, E2> {
35    type Output = T;
36
37    fn unwrap_all(self) -> Self::Output {
38        self.unwrap().unwrap()
39    }
40}
41
42impl<T, E: Debug> UnwrapExt for Option<Result<T, E>> {
43    type Output = T;
44
45    fn unwrap_all(self) -> Self::Output {
46        self.unwrap().unwrap()
47    }
48}
49
50impl<T> UnwrapExt for Option<Option<T>> {
51    type Output = T;
52
53    fn unwrap_all(self) -> Self::Output {
54        self.unwrap().unwrap()
55    }
56}