hlist2/ops/
shuffle.rs

1use crate::{HList, Nil};
2
3use super::{ManyIndex, RemoveMany};
4
5/// Shuffle current heterogenous list, or change order of its elements.
6///
7/// Index generic parameter is used to determine a way to construct shuffled list,
8/// because there may be more than one if type of elements is not unique.
9pub trait Shuffle<T, I>: RemoveMany<T, I, Remainder = Nil>
10where
11    T: HList,
12    I: ManyIndex,
13{
14    /// Shuffles current heterogenous list, or changes order of its elements.
15    ///
16    /// # Examples
17    ///
18    /// ```
19    /// use hlist2::{hlist, HList, ops::Shuffle};
20    ///
21    /// let list = hlist![1, 2.0, true, "hello world"];
22    /// let shuffled: HList![bool, i32, &str, f32] = list.shuffle();
23    /// assert_eq!(shuffled, hlist![true, 1, "hello world", 2.0]);
24    /// ```
25    fn shuffle(self) -> T;
26}
27
28impl<T, L, I> Shuffle<L, I> for T
29where
30    L: HList,
31    T: RemoveMany<L, I, Remainder = Nil>,
32    I: ManyIndex,
33{
34    fn shuffle(self) -> L {
35        let (list, _) = self.remove_many();
36        list
37    }
38}