Skip to content

Commit a69b481

Browse files
tranzystorekknotgull
authored andcommitted
Accept any IntoIterator in choose_multiple
1 parent 1b93479 commit a69b481

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let elem = v[i];
4343
Sample values from an array with `O(n)` complexity (`n` is the length of array):
4444

4545
```rust
46-
fastrand::choose_multiple(vec![1, 4, 5].iter(), 2);
46+
fastrand::choose_multiple([1, 4, 5], 2);
4747
fastrand::choose_multiple(0..20, 12);
4848
```
4949

src/global_rng.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ pub fn f64() -> f64 {
181181
with_rng(|r| r.f64())
182182
}
183183

184-
/// Collects `amount` values at random from the iterator into a vector.
185-
pub fn choose_multiple<T: Iterator>(source: T, amount: usize) -> Vec<T::Item> {
184+
/// Collects `amount` values at random from the iterable into a vector.
185+
pub fn choose_multiple<I: IntoIterator>(source: I, amount: usize) -> Vec<I::Item> {
186186
with_rng(|rng| rng.choose_multiple(source, amount))
187187
}
188188

src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! Sample values from an array with `O(n)` complexity (`n` is the length of array):
3333
//!
3434
//! ```
35-
//! fastrand::choose_multiple(vec![1, 4, 5].iter(), 2);
35+
//! fastrand::choose_multiple([1, 4, 5], 2);
3636
//! fastrand::choose_multiple(0..20, 12);
3737
//! ```
3838
//!
@@ -378,27 +378,28 @@ impl Rng {
378378
f64::from_bits((1 << (b - 2)) - (1 << f) + (self.u64(..) >> (b - f))) - 1.0
379379
}
380380

381-
/// Collects `amount` values at random from the iterator into a vector.
381+
/// Collects `amount` values at random from the iterable into a vector.
382382
///
383-
/// The length of the returned vector equals `amount` unless the iterator
383+
/// The length of the returned vector equals `amount` unless the iterable
384384
/// contains insufficient elements, in which case it equals the number of
385385
/// elements available.
386386
///
387-
/// Complexity is `O(n)` where `n` is the length of the iterator.
387+
/// Complexity is `O(n)` where `n` is the length of the iterable.
388388
#[cfg(feature = "alloc")]
389389
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
390-
pub fn choose_multiple<T: Iterator>(&mut self, mut source: T, amount: usize) -> Vec<T::Item> {
390+
pub fn choose_multiple<I: IntoIterator>(&mut self, source: I, amount: usize) -> Vec<I::Item> {
391391
// Adapted from: https://docs.rs/rand/latest/rand/seq/trait.IteratorRandom.html#method.choose_multiple
392392
let mut reservoir = Vec::with_capacity(amount);
393+
let mut iter = source.into_iter();
393394

394-
reservoir.extend(source.by_ref().take(amount));
395+
reservoir.extend(iter.by_ref().take(amount));
395396

396397
// Continue unless the iterator was exhausted
397398
//
398399
// note: this prevents iterators that "restart" from causing problems.
399400
// If the iterator stops once, then so do we.
400401
if reservoir.len() == amount {
401-
for (i, elem) in source.enumerate() {
402+
for (i, elem) in iter.enumerate() {
402403
let end = i + 1 + amount;
403404
let k = self.usize(0..end);
404405
if let Some(slot) = reservoir.get_mut(k) {

0 commit comments

Comments
 (0)