Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FusedIterator and ExactSizeIterator where possible #159

Merged
merged 1 commit into from Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion onig/src/find.rs
@@ -1,5 +1,5 @@
use super::{Regex, Region, SearchOptions};
use std::iter::Iterator;
use std::iter::FusedIterator;

impl Regex {
/// Returns the capture groups corresponding to the leftmost-first match
Expand Down Expand Up @@ -299,8 +299,16 @@ impl<'t> Iterator for SubCaptures<'t> {
let size = self.caps.len();
(size, Some(size))
}

fn count(self) -> usize {
self.caps.len()
}
}

impl<'t> FusedIterator for SubCaptures<'t> {}

impl<'t> ExactSizeIterator for SubCaptures<'t> {}

/// An iterator over capture group positions for a particular match of
/// a regular expression.
///
Expand All @@ -327,8 +335,16 @@ impl<'t> Iterator for SubCapturesPos<'t> {
let size = self.caps.len();
(size, Some(size))
}

fn count(self) -> usize {
self.caps.len()
}
}

impl<'t> FusedIterator for SubCapturesPos<'t> {}

impl<'t> ExactSizeIterator for SubCapturesPos<'t> {}

/// An iterator over all non-overlapping matches for a particular string.
///
/// The iterator yields a tuple of integers corresponding to the start and end
Expand Down Expand Up @@ -380,6 +396,8 @@ impl<'r, 't> Iterator for FindMatches<'r, 't> {
}
}

impl<'r, 't> FusedIterator for FindMatches<'r, 't> {}

/// An iterator that yields all non-overlapping capture groups matching a
/// particular regular expression.
///
Expand Down Expand Up @@ -433,6 +451,8 @@ impl<'r, 't> Iterator for FindCaptures<'r, 't> {
}
}

impl<'r, 't> FusedIterator for FindCaptures<'r, 't> {}

/// Yields all substrings delimited by a regular expression match.
///
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
Expand Down Expand Up @@ -466,6 +486,8 @@ impl<'r, 't> Iterator for RegexSplits<'r, 't> {
}
}

impl<'r, 't> FusedIterator for RegexSplits<'r, 't> {}

/// Yields at most `N` substrings delimited by a regular expression match.
///
/// The last substring will be whatever remains after splitting.
Expand Down Expand Up @@ -498,6 +520,8 @@ impl<'r, 't> Iterator for RegexSplitsN<'r, 't> {
}
}

impl<'r, 't> FusedIterator for RegexSplitsN<'r, 't> {}

#[cfg(test)]
mod tests {
use super::super::*;
Expand Down
5 changes: 5 additions & 0 deletions onig/src/region.rs
@@ -1,6 +1,7 @@
#![allow(clippy::transmute_ptr_to_ref)]

use onig_sys;
use std::iter::FusedIterator;
use std::mem::transmute;
use std::os::raw::{c_int, c_void};
use std::ptr::null_mut;
Expand Down Expand Up @@ -252,6 +253,10 @@ impl<'a> Iterator for RegionIter<'a> {
}
}

impl<'a> FusedIterator for RegionIter<'a> {}

impl<'a> ExactSizeIterator for RegionIter<'a> {}

#[cfg(test)]
mod tests {
use super::super::{Regex, SearchOptions};
Expand Down
10 changes: 9 additions & 1 deletion onig/src/tree.rs
@@ -1,7 +1,7 @@
#![allow(clippy::transmute_ptr_to_ref)]

use onig_sys;
use std::iter::Iterator;
use std::iter::FusedIterator;
use std::mem::transmute;
use std::ops::Index;

Expand Down Expand Up @@ -77,8 +77,16 @@ impl<'t> Iterator for CaptureTreeNodeIter<'t> {
let size = self.node.len();
(size, Some(size))
}

fn count(self) -> usize {
self.node.len()
}
}

impl<'t> FusedIterator for CaptureTreeNodeIter<'t> {}

impl<'t> ExactSizeIterator for CaptureTreeNodeIter<'t> {}

#[cfg(test)]
mod tests {
use super::super::*;
Expand Down