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