tuplities_from/
lib.rs

1//! [tuplities](https://github.com/lucacappelletti94/tuplities) suite crate providing the `TupleFrom` and `TupleInto` traits.
2
3#[tuplities_derive::impl_tuple_from]
4/// A trait for infallibly converting from one tuple type to another.
5///
6/// This trait allows converting between tuples where each element can be converted
7/// using the standard `From` trait. Unlike `TupleTryFrom`, this trait guarantees
8/// that the conversion will succeed.
9///
10/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
11pub trait TupleFrom<T> {
12    /// Converts from `T` into `Self`.
13    ///
14    /// # Examples
15    ///
16    /// ```rust
17    /// use tuplities_from::TupleFrom;
18    ///
19    /// let source = (1u8, 2u8);
20    /// let target: (u32, u32) = TupleFrom::tuple_from(source);
21    /// assert_eq!(target, (1, 2));
22    /// ```
23    fn tuple_from(value: T) -> Self;
24}
25
26/// A trait for infallibly converting a tuple into another tuple type.
27///
28/// This trait allows converting tuples into other tuple types where each element can be converted
29/// using the standard `From` trait. Unlike `TupleTryInto`, this trait guarantees
30/// that the conversion will succeed.
31///
32/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
33pub trait TupleInto<T> {
34    /// Converts `self` into `T`.
35    ///
36    /// # Examples
37    ///
38    /// ```rust
39    /// use tuplities_from::TupleInto;
40    ///
41    /// let source = (1u8, 2u8);
42    /// let target: (u32, u32) = TupleInto::tuple_into(source);
43    /// assert_eq!(target, (1, 2));
44    /// ```
45    fn tuple_into(self) -> T;
46}
47
48// Blanket implementation of TupleInto based on TupleFrom
49impl<T, U> TupleInto<T> for U
50where
51    T: TupleFrom<U>,
52{
53    fn tuple_into(self) -> T {
54        T::tuple_from(self)
55    }
56}