1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

shuf: Use impl return type in trait now that MSRV is high enough

This commit is contained in:
Jan Verbeek 2025-03-16 13:42:17 +01:00
parent cdd1052cea
commit e830dd45f0

View file

@ -243,17 +243,11 @@ trait Shufable {
type Item: Writable; type Item: Writable;
fn is_empty(&self) -> bool; fn is_empty(&self) -> bool;
fn choose(&self, rng: &mut WrappedRng) -> Self::Item; fn choose(&self, rng: &mut WrappedRng) -> Self::Item;
// This type shouldn't even be known. However, because we want to support
// Rust 1.70, it is not possible to return "impl Iterator".
// TODO: When the MSRV is raised, rewrite this to return "impl Iterator".
type PartialShuffleIterator<'b>: Iterator<Item = Self::Item>
where
Self: 'b;
fn partial_shuffle<'b>( fn partial_shuffle<'b>(
&'b mut self, &'b mut self,
rng: &'b mut WrappedRng, rng: &'b mut WrappedRng,
amount: usize, amount: usize,
) -> Self::PartialShuffleIterator<'b>; ) -> impl Iterator<Item = Self::Item>;
} }
impl<'a> Shufable for Vec<&'a [u8]> { impl<'a> Shufable for Vec<&'a [u8]> {
@ -267,15 +261,11 @@ impl<'a> Shufable for Vec<&'a [u8]> {
// this is safe. // this is safe.
(**self).choose(rng).unwrap() (**self).choose(rng).unwrap()
} }
type PartialShuffleIterator<'b>
= std::iter::Copied<std::slice::Iter<'b, &'a [u8]>>
where
Self: 'b;
fn partial_shuffle<'b>( fn partial_shuffle<'b>(
&'b mut self, &'b mut self,
rng: &'b mut WrappedRng, rng: &'b mut WrappedRng,
amount: usize, amount: usize,
) -> Self::PartialShuffleIterator<'b> { ) -> impl Iterator<Item = Self::Item> {
// Note: "copied()" only copies the reference, not the entire [u8]. // Note: "copied()" only copies the reference, not the entire [u8].
(**self).partial_shuffle(rng, amount).0.iter().copied() (**self).partial_shuffle(rng, amount).0.iter().copied()
} }
@ -289,12 +279,11 @@ impl<'a> Shufable for Vec<&'a OsStr> {
fn choose(&self, rng: &mut WrappedRng) -> Self::Item { fn choose(&self, rng: &mut WrappedRng) -> Self::Item {
(**self).choose(rng).unwrap() (**self).choose(rng).unwrap()
} }
type PartialShuffleIterator<'b> = std::iter::Copied<std::slice::Iter<'b, &'a OsStr>> where Self: 'b;
fn partial_shuffle<'b>( fn partial_shuffle<'b>(
&'b mut self, &'b mut self,
rng: &'b mut WrappedRng, rng: &'b mut WrappedRng,
amount: usize, amount: usize,
) -> Self::PartialShuffleIterator<'b> { ) -> impl Iterator<Item = Self::Item> {
(**self).partial_shuffle(rng, amount).0.iter().copied() (**self).partial_shuffle(rng, amount).0.iter().copied()
} }
} }
@ -307,15 +296,11 @@ impl Shufable for RangeInclusive<usize> {
fn choose(&self, rng: &mut WrappedRng) -> usize { fn choose(&self, rng: &mut WrappedRng) -> usize {
rng.random_range(self.clone()) rng.random_range(self.clone())
} }
type PartialShuffleIterator<'b>
= NonrepeatingIterator<'b>
where
Self: 'b;
fn partial_shuffle<'b>( fn partial_shuffle<'b>(
&'b mut self, &'b mut self,
rng: &'b mut WrappedRng, rng: &'b mut WrappedRng,
amount: usize, amount: usize,
) -> Self::PartialShuffleIterator<'b> { ) -> impl Iterator<Item = Self::Item> {
NonrepeatingIterator::new(self.clone(), rng, amount) NonrepeatingIterator::new(self.clone(), rng, amount)
} }
} }