tuplities_option/
lib.rs

1#![no_std]
2
3//! [tuplities](https://github.com/lucacappelletti94/tuplities) suite crate providing the `TupleOption` and `IntoTupleOption` traits.
4
5use tuplities_default::TupleDefault;
6
7#[tuplities_derive::impl_tuple_option]
8/// A trait for transposing a tuple of options into an option of a tuple.
9pub trait TupleOption: TupleDefault {
10    /// The transposed type: an option of the tuple of the inner types.
11    type Transposed: IntoTupleOption<IntoOptions = Self>;
12
13    /// Transposes the tuple of options into an option of the tuple.
14    ///
15    /// Returns `Some((a, b, ...))` if all elements are `Some`, otherwise `None`.
16    ///
17    /// # Examples
18    ///
19    /// ```rust
20    /// use tuplities_option::TupleOption;
21    ///
22    /// let tuple = (Some(1), Some(2));
23    /// let transposed: Option<(i32, i32)> = tuple.transpose();
24    /// assert_eq!(transposed, Some((1, 2)));
25    ///
26    /// let tuple = (Some(1), None);
27    /// let transposed: Option<(i32, i32)> = tuple.transpose();
28    /// assert_eq!(transposed, None);
29    /// ```
30    ///
31    /// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
32    fn transpose(self) -> Option<Self::Transposed>;
33}
34
35/// A trait for converting a tuple into a tuple of options.
36pub trait IntoTupleOption {
37    /// The tuple of options type.
38    type IntoOptions: TupleOption<Transposed = Self> + TupleDefault;
39
40    /// Converts the tuple into a tuple of `Some` values.
41    ///
42    /// # Examples
43    ///
44    /// ```rust
45    /// use tuplities_option::IntoTupleOption;
46    ///
47    /// let tuple = (1, 2);
48    /// let into_options: (Option<i32>, Option<i32>) = tuple.into_options();
49    /// assert_eq!(into_options, (Some(1), Some(2)));
50    /// ```
51    ///
52    /// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
53    fn into_options(self) -> Self::IntoOptions;
54}