Implement more methods for custom iterators.

This commit is contained in:
Stephen Chung 2021-07-12 13:05:38 +08:00
parent fe6691752b
commit 713f8ea672

View File

@ -1,5 +1,6 @@
use crate::dynamic::Variant; use crate::dynamic::Variant;
use crate::{def_package, EvalAltResult, INT}; use crate::{def_package, EvalAltResult, INT};
use std::iter::{ExactSizeIterator, FusedIterator};
use std::ops::Range; use std::ops::Range;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -127,6 +128,11 @@ where
} }
} }
impl<T> FusedIterator for StepRange<T> where
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>
{
}
// Bit-field iterator with step // Bit-field iterator with step
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
struct BitRange(INT, INT, usize); struct BitRange(INT, INT, usize);
@ -190,6 +196,20 @@ impl Iterator for BitRange {
Some(r) Some(r)
} }
} }
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.2, Some(self.2))
}
}
impl FusedIterator for BitRange {}
impl ExactSizeIterator for BitRange {
#[inline(always)]
fn len(&self) -> usize {
self.2
}
} }
// String iterator over characters // String iterator over characters
@ -248,6 +268,21 @@ impl Iterator for CharsStream {
Some(ch) Some(ch)
} }
} }
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.0.len() - self.1;
(remaining, Some(remaining))
}
}
impl FusedIterator for CharsStream {}
impl ExactSizeIterator for CharsStream {
#[inline(always)]
fn len(&self) -> usize {
self.0.len() - self.1
}
} }
macro_rules! reg_range { macro_rules! reg_range {
@ -356,7 +391,7 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
} }
} }
impl std::iter::FusedIterator for StepFloatRange {} impl FusedIterator for StepFloatRange {}
lib.set_iterator::<StepFloatRange>(); lib.set_iterator::<StepFloatRange>();
@ -418,7 +453,7 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
} }
} }
impl std::iter::FusedIterator for StepDecimalRange {} impl FusedIterator for StepDecimalRange {}
lib.set_iterator::<StepDecimalRange>(); lib.set_iterator::<StepDecimalRange>();