multiversx_sc/
non_zero_util.rsuse core::{iter::Iterator, num::NonZeroUsize};
#[inline]
pub const fn non_zero_usize_one() -> NonZeroUsize {
unsafe { NonZeroUsize::new_unchecked(1) }
}
#[inline]
pub const fn non_zero_usize_from_n_plus_1(n: usize) -> NonZeroUsize {
unsafe { NonZeroUsize::new_unchecked(n + 1) }
}
#[inline]
pub const fn non_zero_usize_plus(a: NonZeroUsize, b: usize) -> NonZeroUsize {
unsafe { NonZeroUsize::new_unchecked(a.get() + b) }
}
pub struct NonZeroUsizeIterator {
prev_num: usize,
limit: usize,
}
impl NonZeroUsizeIterator {
pub fn from_1_to_n(n: usize) -> Self {
NonZeroUsizeIterator {
prev_num: 0,
limit: n,
}
}
}
impl Iterator for NonZeroUsizeIterator {
type Item = NonZeroUsize;
fn next(&mut self) -> Option<NonZeroUsize> {
if self.prev_num >= self.limit {
return None;
}
self.prev_num += 1;
unsafe { Some(NonZeroUsize::new_unchecked(self.prev_num)) }
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use alloc::vec::Vec;
#[test]
fn test_iter_0() {
let mut v = Vec::<usize>::new();
for nz in NonZeroUsizeIterator::from_1_to_n(0) {
v.push(nz.get());
}
assert_eq!(v, [].to_vec());
}
#[test]
fn test_iter_1() {
let mut v = Vec::<usize>::new();
for nz in NonZeroUsizeIterator::from_1_to_n(1) {
v.push(nz.get());
}
assert_eq!(v, [1].to_vec());
}
#[test]
fn test_iter_3() {
let mut v = Vec::<usize>::new();
for nz in NonZeroUsizeIterator::from_1_to_n(3) {
v.push(nz.get());
}
assert_eq!(v, [1, 2, 3].to_vec());
}
const ONE: NonZeroUsize = non_zero_usize_one();
const TWO: NonZeroUsize = non_zero_usize_from_n_plus_1(1);
const TEN: NonZeroUsize = non_zero_usize_plus(ONE, 9);
#[test]
fn test_const() {
assert_eq!(ONE.get(), 1);
assert_eq!(TWO.get(), 2);
assert_eq!(TEN.get(), 10);
}
}