-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new operation to shuffle heterogenous list
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::{HList, Nil}; | ||
|
||
use super::{RemoveMany, RemoveManyIndex}; | ||
|
||
/// Shuffle current heterogenous list, or change order of its elements. | ||
/// | ||
/// Index generic parameter is used to determine a way to construct shuffled list, | ||
/// because there may be more than one if type of elements is not unique. | ||
pub trait Shuffle<T, I>: RemoveMany<T, I, Remainder = Nil> | ||
where | ||
T: HList, | ||
I: RemoveManyIndex, | ||
{ | ||
/// Shuffles current heterogenous list, or changes order of its elements. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use hlist2::{hlist, HList, ops::Shuffle}; | ||
/// | ||
/// let list = hlist![1, 2.0, true, "hello world"]; | ||
/// let shuffled: HList![bool, i32, &str, f32] = list.shuffle(); | ||
/// assert_eq!(shuffled, hlist![true, 1, "hello world", 2.0]); | ||
/// ``` | ||
fn shuffle(self) -> T; | ||
} | ||
|
||
impl<T, L, I> Shuffle<L, I> for T | ||
where | ||
L: HList, | ||
T: RemoveMany<L, I, Remainder = Nil>, | ||
I: RemoveManyIndex, | ||
{ | ||
fn shuffle(self) -> L { | ||
let (list, _) = self.remove_many(); | ||
list | ||
} | ||
} |