From 713f8ea672a0ba47bcb278f814194d17b3c0d285 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 12 Jul 2021 13:05:38 +0800 Subject: [PATCH] Implement more methods for custom iterators. --- src/packages/iter_basic.rs | 39 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 835030fe..94a8398c 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -1,5 +1,6 @@ use crate::dynamic::Variant; use crate::{def_package, EvalAltResult, INT}; +use std::iter::{ExactSizeIterator, FusedIterator}; use std::ops::Range; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -127,6 +128,11 @@ where } } +impl FusedIterator for StepRange where + T: Variant + Copy + PartialOrd + Add + Sub +{ +} + // Bit-field iterator with step #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] struct BitRange(INT, INT, usize); @@ -190,6 +196,20 @@ impl Iterator for BitRange { Some(r) } } + + #[inline(always)] + fn size_hint(&self) -> (usize, Option) { + (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 @@ -248,6 +268,21 @@ impl Iterator for CharsStream { Some(ch) } } + + #[inline(always)] + fn size_hint(&self) -> (usize, Option) { + 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 { @@ -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::(); @@ -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::();