tuplities_ref/
lib.rs

1#![no_std]
2
3//! [tuplities](https://github.com/lucacappelletti94/tuplities) suite crate providing the `TupleRef` trait.
4
5#[tuplities_derive::impl_tuple_ref]
6/// A trait for tuples that provides a method to get a tuple of references.
7///
8/// This trait provides both an associated type `Ref<'a>` that represents a tuple
9/// of references to the elements, and a method `tuple_ref` that returns such a tuple.
10///
11/// # Examples
12///
13/// ```rust
14/// use tuplities_ref::TupleRef;
15///
16/// let tuple = (1, "hello".to_string(), vec![1, 2, 3]);
17/// let refs = tuple.tuple_ref();
18/// assert_eq!(refs, (&1, &"hello".to_string(), &vec![1, 2, 3]));
19/// ```
20///
21/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
22pub trait TupleRef {
23    /// The type of a tuple containing references to each element.
24    type Ref<'a>
25    where
26        Self: 'a;
27
28    /// Returns a tuple of references to each element.
29    ///
30    /// # Examples
31    ///
32    /// ```rust
33    /// use tuplities_ref::TupleRef;
34    ///
35    /// let tuple = (42, "world");
36    /// let refs = tuple.tuple_ref();
37    /// assert_eq!(refs, (&42, &"world"));
38    /// ```
39    fn tuple_ref(&self) -> Self::Ref<'_>;
40}
41
42#[tuplities_derive::impl_tuple_ref_map]
43
44/// A trait for applying `TupleRef` to each element of a tuple.
45///
46/// This trait takes a tuple where each element implements `TupleRef` and returns
47/// a tuple where each element is the result of calling `tuple_ref()` on the original elements.
48///
49/// # Examples
50///
51/// ```rust
52/// use tuplities_ref::TupleRefMap;
53///
54/// let matrix = ((1, 2), (3, 4), (5, 6));
55/// let ref_matrix = matrix.tuple_ref_map();
56/// assert_eq!(ref_matrix, ((&1, &2), (&3, &4), (&5, &6)));
57/// ```
58///
59/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
60pub trait TupleRefMap {
61    /// The type of a tuple containing tuples of references to each inner element.
62    type RefMap<'a>
63    where
64        Self: 'a;
65
66    /// Returns a tuple where each element is a tuple of references to the inner elements.
67    fn tuple_ref_map(&self) -> Self::RefMap<'_>;
68}