1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
crate::do_impl!("all-some", tuple_all_some, {
/// The type when a tuple of [Option]s is element-wise unwrapped.
///
/// ```
/// use same_types::assert_same_types;
/// use tupleops::AllSome;
///
/// assert_same_types!(
/// AllSome<(Option<u8>, Option<u16>, Option<u32>)>,
/// (u8, u16, u32),
/// );
/// ```
///
/// See also: [all_some()], [TupleAllSome].
#[cfg_attr(docsrs, doc(cfg(feature = "all-some")))]
pub type AllSome<Tpl> = <Tpl as TupleAllSome<Tpl>>::Type;
/// Element-wise unwrap a tuple of [Option]s if all elements are good.
/// Return the input otherwise.
///
/// ```
/// use tupleops::all_some;
///
/// assert_eq!(
/// all_some((Some(1), Some(2), Some(3))),
/// Ok((1, 2, 3)),
/// );
///
/// assert_eq!(
/// all_some((Some(1), Option::<i32>::None, Some(3))),
/// Err((Some(1), None, Some(3))),
/// );
///
/// assert_eq!(
/// all_some(()),
/// Ok(()),
/// );
/// ```
///
/// See also: [AllSome], [TupleAllSome].
#[cfg_attr(docsrs, doc(cfg(feature = "all-some")))]
#[inline(always)]
pub fn all_some<Tpl>(tpl: Tpl) -> Result<AllSome<Tpl>, Tpl>
where
Tpl: TupleAllSome<Tpl>,
{
<Tpl as TupleAllSome<Tpl>>::all_some(tpl)
}
/// A tuple that is usable with [all_some()].
///
/// See also: [all_some()], [TupleAllSome].
#[cfg_attr(docsrs, doc(cfg(feature = "all-some")))]
pub trait TupleAllSome<Tpl> {
#[doc(hidden)]
type Type;
#[doc(hidden)]
fn all_some(tpl: Tpl) -> Result<Self::Type, Tpl>;
}
impl TupleAllSome<()> for () {
type Type = ();
#[doc(hidden)]
fn all_some(tpl: ()) -> Result<Self::Type, ()> {
let () = tpl;
Ok(())
}
}
});