Skip to content

Commit

Permalink
Add new operation to shuffle heterogenous list
Browse files Browse the repository at this point in the history
  • Loading branch information
tuguzT committed Sep 7, 2023
1 parent 5285d54 commit 9f90d01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use self::{
remove::Remove,
remove_many::{RemoveMany, RemoveManyIndex},
reverse::Reverse,
shuffle::Shuffle,
to_ref::ToRef,
unzip::Unzip,
zip::Zip,
Expand All @@ -32,6 +33,7 @@ mod prepend;
mod remove;
mod remove_many;
mod reverse;
mod shuffle;
mod to_ref;
mod unzip;
mod zip;
38 changes: 38 additions & 0 deletions src/ops/shuffle.rs
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
}
}

0 comments on commit 9f90d01

Please sign in to comment.