|
32 | 32 | //! Sample values from an array with `O(n)` complexity (`n` is the length of array): |
33 | 33 | //! |
34 | 34 | //! ``` |
35 | | -//! fastrand::choose_multiple(vec![1, 4, 5].iter(), 2); |
| 35 | +//! fastrand::choose_multiple([1, 4, 5], 2); |
36 | 36 | //! fastrand::choose_multiple(0..20, 12); |
37 | 37 | //! ``` |
38 | 38 | //! |
@@ -378,27 +378,28 @@ impl Rng { |
378 | 378 | f64::from_bits((1 << (b - 2)) - (1 << f) + (self.u64(..) >> (b - f))) - 1.0 |
379 | 379 | } |
380 | 380 |
|
381 | | - /// Collects `amount` values at random from the iterator into a vector. |
| 381 | + /// Collects `amount` values at random from the iterable into a vector. |
382 | 382 | /// |
383 | | - /// The length of the returned vector equals `amount` unless the iterator |
| 383 | + /// The length of the returned vector equals `amount` unless the iterable |
384 | 384 | /// contains insufficient elements, in which case it equals the number of |
385 | 385 | /// elements available. |
386 | 386 | /// |
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. |
388 | 388 | #[cfg(feature = "alloc")] |
389 | 389 | #[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> { |
391 | 391 | // Adapted from: https://docs.rs/rand/latest/rand/seq/trait.IteratorRandom.html#method.choose_multiple |
392 | 392 | let mut reservoir = Vec::with_capacity(amount); |
| 393 | + let mut iter = source.into_iter(); |
393 | 394 |
|
394 | | - reservoir.extend(source.by_ref().take(amount)); |
| 395 | + reservoir.extend(iter.by_ref().take(amount)); |
395 | 396 |
|
396 | 397 | // Continue unless the iterator was exhausted |
397 | 398 | // |
398 | 399 | // note: this prevents iterators that "restart" from causing problems. |
399 | 400 | // If the iterator stops once, then so do we. |
400 | 401 | if reservoir.len() == amount { |
401 | | - for (i, elem) in source.enumerate() { |
| 402 | + for (i, elem) in iter.enumerate() { |
402 | 403 | let end = i + 1 + amount; |
403 | 404 | let k = self.usize(0..end); |
404 | 405 | if let Some(slot) = reservoir.get_mut(k) { |
|
0 commit comments