|
| 1 | +use crate::iter::{FusedIterator, TrustedLen}; |
| 2 | +use crate::mem::ManuallyDrop; |
| 3 | + |
| 4 | +/// Creates a new iterator that repeats a single element a given number of times. |
| 5 | +/// |
| 6 | +/// The `repeat_n()` function repeats a single value exactly `n` times. |
| 7 | +/// |
| 8 | +/// This is very similar to using [`repeat()`] with [`Iterator::take()`], |
| 9 | +/// but there are two differences: |
| 10 | +/// - `repeat_n()` can return the original value, rather than always cloning. |
| 11 | +/// - `repeat_n()` produces an [`ExactSizeIterator`]. |
| 12 | +/// |
| 13 | +/// [`repeat()`]: crate::iter::repeat |
| 14 | +/// |
| 15 | +/// # Examples |
| 16 | +/// |
| 17 | +/// Basic usage: |
| 18 | +/// |
| 19 | +/// ``` |
| 20 | +/// #![feature(iter_repeat_n)] |
| 21 | +/// use std::iter; |
| 22 | +/// |
| 23 | +/// // four of the the number four: |
| 24 | +/// let mut four_fours = iter::repeat_n(4, 4); |
| 25 | +/// |
| 26 | +/// assert_eq!(Some(4), four_fours.next()); |
| 27 | +/// assert_eq!(Some(4), four_fours.next()); |
| 28 | +/// assert_eq!(Some(4), four_fours.next()); |
| 29 | +/// assert_eq!(Some(4), four_fours.next()); |
| 30 | +/// |
| 31 | +/// // no more fours |
| 32 | +/// assert_eq!(None, four_fours.next()); |
| 33 | +/// ``` |
| 34 | +/// |
| 35 | +/// For non-`Copy` types, |
| 36 | +/// |
| 37 | +/// ``` |
| 38 | +/// #![feature(iter_repeat_n)] |
| 39 | +/// use std::iter; |
| 40 | +/// |
| 41 | +/// let v: Vec<i32> = Vec::with_capacity(123); |
| 42 | +/// let mut it = iter::repeat_n(v, 5); |
| 43 | +/// |
| 44 | +/// for i in 0..4 { |
| 45 | +/// // It starts by cloning things |
| 46 | +/// let cloned = it.next().unwrap(); |
| 47 | +/// assert_eq!(cloned.len(), 0); |
| 48 | +/// assert_eq!(cloned.capacity(), 0); |
| 49 | +/// } |
| 50 | +/// |
| 51 | +/// // ... but the last item is the original one |
| 52 | +/// let last = it.next().unwrap(); |
| 53 | +/// assert_eq!(last.len(), 0); |
| 54 | +/// assert_eq!(last.capacity(), 123); |
| 55 | +/// |
| 56 | +/// // ... and now we're done |
| 57 | +/// assert_eq!(None, it.next()); |
| 58 | +/// ``` |
| 59 | +#[inline] |
| 60 | +#[unstable( |
| 61 | + feature = "iter_repeat_n", |
| 62 | + reason = "waiting on FCP to decide whether to expose publicly", |
| 63 | + issue = "104434" |
| 64 | +)] |
| 65 | +pub fn repeat_n<T: Clone>(element: T, count: usize) -> RepeatN<T> { |
| 66 | + let mut element = ManuallyDrop::new(element); |
| 67 | + |
| 68 | + if count == 0 { |
| 69 | + // SAFETY: we definitely haven't dropped it yet, since we only just got |
| 70 | + // passed it in, and because the count is zero the instance we're about |
| 71 | + // to create won't drop it, so to avoid leaking we need to now. |
| 72 | + unsafe { ManuallyDrop::drop(&mut element) }; |
| 73 | + } |
| 74 | + |
| 75 | + RepeatN { element, count } |
| 76 | +} |
| 77 | + |
| 78 | +/// An iterator that repeats an element an exact number of times. |
| 79 | +/// |
| 80 | +/// This `struct` is created by the [`repeat_n()`] function. |
| 81 | +/// See its documentation for more. |
| 82 | +#[derive(Clone, Debug)] |
| 83 | +#[unstable( |
| 84 | + feature = "iter_repeat_n", |
| 85 | + reason = "waiting on FCP to decide whether to expose publicly", |
| 86 | + issue = "104434" |
| 87 | +)] |
| 88 | +pub struct RepeatN<A> { |
| 89 | + count: usize, |
| 90 | + // Invariant: has been dropped iff count == 0. |
| 91 | + element: ManuallyDrop<A>, |
| 92 | +} |
| 93 | + |
| 94 | +impl<A> RepeatN<A> { |
| 95 | + /// If we haven't already dropped the element, return it in an option. |
| 96 | + /// |
| 97 | + /// Clears the count so it won't be dropped again later. |
| 98 | + #[inline] |
| 99 | + fn take_element(&mut self) -> Option<A> { |
| 100 | + if self.count > 0 { |
| 101 | + self.count = 0; |
| 102 | + // SAFETY: We just set count to zero so it won't be dropped again, |
| 103 | + // and it used to be non-zero so it hasn't already been dropped. |
| 104 | + unsafe { Some(ManuallyDrop::take(&mut self.element)) } |
| 105 | + } else { |
| 106 | + None |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[unstable(feature = "iter_repeat_n", issue = "104434")] |
| 112 | +impl<A> Drop for RepeatN<A> { |
| 113 | + fn drop(&mut self) { |
| 114 | + self.take_element(); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[unstable(feature = "iter_repeat_n", issue = "104434")] |
| 119 | +impl<A: Clone> Iterator for RepeatN<A> { |
| 120 | + type Item = A; |
| 121 | + |
| 122 | + #[inline] |
| 123 | + fn next(&mut self) -> Option<A> { |
| 124 | + if self.count == 0 { |
| 125 | + return None; |
| 126 | + } |
| 127 | + |
| 128 | + self.count -= 1; |
| 129 | + Some(if self.count == 0 { |
| 130 | + // SAFETY: the check above ensured that the count used to be non-zero, |
| 131 | + // so element hasn't been dropped yet, and we just lowered the count to |
| 132 | + // zero so it won't be dropped later, and thus it's okay to take it here. |
| 133 | + unsafe { ManuallyDrop::take(&mut self.element) } |
| 134 | + } else { |
| 135 | + A::clone(&mut self.element) |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | + #[inline] |
| 140 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 141 | + let len = self.len(); |
| 142 | + (len, Some(len)) |
| 143 | + } |
| 144 | + |
| 145 | + #[inline] |
| 146 | + fn advance_by(&mut self, skip: usize) -> Result<(), usize> { |
| 147 | + let len = self.count; |
| 148 | + |
| 149 | + if skip >= len { |
| 150 | + self.take_element(); |
| 151 | + } |
| 152 | + |
| 153 | + if skip > len { |
| 154 | + Err(len) |
| 155 | + } else { |
| 156 | + self.count = len - skip; |
| 157 | + Ok(()) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + #[inline] |
| 162 | + fn last(mut self) -> Option<A> { |
| 163 | + self.take_element() |
| 164 | + } |
| 165 | + |
| 166 | + #[inline] |
| 167 | + fn count(self) -> usize { |
| 168 | + self.len() |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +#[unstable(feature = "iter_repeat_n", issue = "104434")] |
| 173 | +impl<A: Clone> ExactSizeIterator for RepeatN<A> { |
| 174 | + fn len(&self) -> usize { |
| 175 | + self.count |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +#[unstable(feature = "iter_repeat_n", issue = "104434")] |
| 180 | +impl<A: Clone> DoubleEndedIterator for RepeatN<A> { |
| 181 | + #[inline] |
| 182 | + fn next_back(&mut self) -> Option<A> { |
| 183 | + self.next() |
| 184 | + } |
| 185 | + |
| 186 | + #[inline] |
| 187 | + fn advance_back_by(&mut self, n: usize) -> Result<(), usize> { |
| 188 | + self.advance_by(n) |
| 189 | + } |
| 190 | + |
| 191 | + #[inline] |
| 192 | + fn nth_back(&mut self, n: usize) -> Option<A> { |
| 193 | + self.nth(n) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +#[unstable(feature = "iter_repeat_n", issue = "104434")] |
| 198 | +impl<A: Clone> FusedIterator for RepeatN<A> {} |
| 199 | + |
| 200 | +#[unstable(feature = "trusted_len", issue = "37572")] |
| 201 | +unsafe impl<A: Clone> TrustedLen for RepeatN<A> {} |
0 commit comments