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