1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
#![no_std]
#![feature(step_trait)]
//! Iterator utility for counting the number of iterations with an arbitrary
//! type that implements the [`Step`] trait.
use core::iter::Step;
/// Consumes the iterator, counting the number of iterations.
/// This uses the [`Step`] trait to keep track of the count of iterations.
/// The count starts from the default value provided by the [`Default`] trait.
///
/// _Note_: This function takes any implementation of [`IntoIterator`],
/// which includes iterators themselves.
///
/// # Panics
///
/// Panics if the count exceeds the capacity of the count type (`T`).
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::step_count;
/// let arr = [1, 2, 3];
/// let count: usize = step_count(arr);
/// assert_eq!(count, 3);
/// ```
///
/// Overflow:
///
/// ```should_panic
/// # use step_count::step_count;
/// let arr = [(); u8::MAX as usize + 1];
/// step_count::<u8>(arr);
/// ```
#[inline]
pub fn step_count<T: Step + Default>(iter: impl IntoIterator) -> T {
step_count_from(iter, T::default())
}
/// Consumes the iterator, counting the number of iterations,
/// starting from a given value.
/// This uses the [`Step`] trait to keep track of the count of iterations.
///
/// _Note_: This function takes any implementation of [`IntoIterator`],
/// which includes iterators themselves.
///
/// # Panics
///
/// Panics if the count exceeds the capacity of the count type (`T`).
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::step_count_from;
/// let arr = [1, 2, 3];
/// let count: u16 = step_count_from(arr, 2);
/// assert_eq!(count, 5);
/// ```
///
/// Overflow:
///
/// ```should_panic
/// # use step_count::step_count_from;
/// let arr = [(); u8::MAX as usize - 1];
/// step_count_from::<u8>(arr, 2);
/// ```
#[inline]
pub fn step_count_from<T: Step>(iter: impl IntoIterator, start: T) -> T {
let iter = iter.into_iter();
iter.fold(start, |count, _| T::forward(count, 1))
}
/// Consumes the iterator, counting the number of iterations.
/// This uses the [`Step`] trait to keep track of the count of iterations.
/// The count starts from the default value provided by the [`Default`] trait.
/// Returns [`None`] if the count exceeded the capcity of the count type (`T`).
///
/// This function always fully consumes the iterator, even if [`None`] is returned.
///
/// _Note_: This function takes any implementation of [`IntoIterator`],
/// which includes iterators themselves.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::step_count_checked;
/// let arr = [1, 2, 3];
/// let count: Option<u16> = step_count_checked(arr);
/// assert_eq!(count, Some(3));
/// ```
///
/// Overflow:
///
/// ```
/// # use step_count::step_count_checked;
/// let arr = [(); u8::MAX as usize + 1];
/// let count: Option<u8> = step_count_checked(arr);
/// assert_eq!(count, None);
/// ```
///
/// Consumption:
///
/// ```
/// # use step_count::step_count_checked;
/// let mut range = -1..u8::MAX as isize;
/// let count: Option<u8> = step_count_checked(&mut range);
/// assert!(range.is_empty());
/// ```
#[inline]
pub fn step_count_checked<T: Step + Default>(iter: impl IntoIterator) -> Option<T> {
step_count_from_checked(iter, T::default())
}
/// Consumes the iterator, counting the number of iterations,
/// starting from a given value.
/// This uses the [`Step`] trait to keep track of the count of iterations.
/// Returns [`None`] if the count exceeded the capcity of the count type (`T`).
///
/// This function always fully consumes the iterator, even if [`None`] is returned.
///
/// _Note_: This function takes any implementation of [`IntoIterator`],
/// which includes iterators themselves.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::step_count_from_checked;
/// let arr = [1, 2, 3];
/// let count: Option<u16> = step_count_from_checked(arr, 2);
/// assert_eq!(count, Some(5));
/// ```
///
/// Overflow:
///
/// ```
/// # use step_count::step_count_from_checked;
/// let arr = [(); u8::MAX as usize - 1];
/// let count: Option<u8> = step_count_from_checked(arr, 2);
/// assert_eq!(count, None);
/// ```
///
/// Consumption:
///
/// ```
/// # use step_count::step_count_from_checked;
/// let mut range = -2..=i8::MAX as isize;
/// let count: Option<i8> = step_count_from_checked(&mut range, -1);
/// assert!(range.is_empty());
/// ```
#[inline]
pub fn step_count_from_checked<T: Step>(iter: impl IntoIterator, start: T) -> Option<T> {
let mut iter = iter.into_iter();
let result = iter.try_fold(start, |count, _| T::forward_checked(count, 1));
iter.last();
result
}
/// Convenience trait to allow using `step_count*` functions as methods.
/// This trait is implemented for every [`Iterator`].
pub trait StepCount: Iterator {
/// Consumes the iterator, counting the number of iterations.
/// This uses the [`Step`] trait to keep track of the count of iterations.
/// The count starts from the default value provided by the [`Default`] trait.
///
/// # Panics
///
/// Panics if the count exceeds the capacity of the count type (`T`).
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::StepCount;
/// let arr = [1, 2, 3];
/// let count: usize = arr.into_iter().step_count();
/// assert_eq!(count, 3);
/// ```
///
/// Overflow:
///
/// ```should_panic
/// # use step_count::StepCount;
/// let range = 0..u8::MAX as usize + 1;
/// range.step_count::<u8>();
/// ```
#[inline]
fn step_count<T: Step + Default>(self) -> T
where
Self: Sized,
{
step_count(self)
}
/// Consumes the iterator, counting the number of iterations,
/// starting from a given value.
/// This uses the [`Step`] trait to keep track of the count of iterations.
///
/// # Panics
///
/// Panics if the count exceeds the capacity of the count type (`T`).
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::StepCount;
/// let arr = [1, 2, 3];
/// let count: u16 = arr.into_iter().step_count_from(2);
/// assert_eq!(count, 5);
/// ```
///
/// Overflow:
///
/// ```should_panic
/// # use step_count::StepCount;
/// let range = 0..u8::MAX as usize - 1;
/// range.step_count_from::<u8>(2);
/// ```
#[inline]
fn step_count_from<T: Step>(self, start: T) -> T
where
Self: Sized,
{
step_count_from(self, start)
}
/// Consumes the iterator, counting the number of iterations.
/// This uses the [`Step`] trait to keep track of the count of iterations.
/// The count starts from the default value provided by the [`Default`] trait.
/// Returns [`None`] if the count exceeded the capcity of the count type (`T`).
///
/// This function always fully consumes the iterator, even if [`None`] is returned.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::StepCount;
/// let arr = [1, 2, 3];
/// let count: Option<u16> = arr.into_iter().step_count_checked();
/// assert_eq!(count, Some(3));
/// ```
///
/// Overflow:
///
/// ```
/// # use step_count::StepCount;
/// let range = 0..u8::MAX as usize + 1;
/// let count: Option<u8> = range.step_count_checked();
/// assert_eq!(count, None);
/// ```
///
/// Consumption:
///
/// ```
/// # use step_count::StepCount;
/// let mut range = -1..u8::MAX as isize;
/// let count: Option<u8> = range.by_ref().step_count_checked();
/// assert!(range.is_empty());
/// ```
#[inline]
fn step_count_checked<T: Step + Default>(self) -> Option<T>
where
Self: Sized,
{
step_count_checked(self)
}
/// Consumes the iterator, counting the number of iterations,
/// starting from a given value.
/// This uses the [`Step`] trait to keep track of the count of iterations.
/// Returns [`None`] if the count exceeded the capcity of the count type (`T`).
///
/// This function always fully consumes the iterator, even if [`None`] is returned.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use step_count::StepCount;
/// let arr = [1, 2, 3];
/// let count: Option<u16> = arr.into_iter().step_count_from_checked(2);
/// assert_eq!(count, Some(5));
/// ```
///
/// Overflow:
///
/// ```
/// # use step_count::StepCount;
/// let range = 0..u8::MAX as usize - 1;
/// let count: Option<u8> = range.step_count_from_checked(2);
/// assert_eq!(count, None);
/// ```
///
/// Consumption:
///
/// ```
/// # use step_count::StepCount;
/// let mut range = -2..i8::MAX as isize;
/// let count: Option<i8> = range.by_ref().step_count_from_checked(-1);
/// assert!(range.is_empty());
/// ```
#[inline]
fn step_count_from_checked<T: Step>(self, start: T) -> Option<T>
where
Self: Sized,
{
step_count_from_checked(self, start)
}
}
impl<T: Iterator> StepCount for T {}