Skip to content

Instantly share code, notes, and snippets.

@U007D
Created May 31, 2017 22:18
Show Gist options
  • Select an option

  • Save U007D/2832c085865bb73d2bffe7d1bc9d9b08 to your computer and use it in GitHub Desktop.

Select an option

Save U007D/2832c085865bb73d2bffe7d1bc9d9b08 to your computer and use it in GitHub Desktop.
Consuming String Iterator Example
extern crate owned_chars
use std::str::Chars;
use owned_chars::{OwnedChars, OwnedCharsExt};
...
#[derive(Debug)]
pub struct Iter<'a> {
iter: Chars<'a>,
}
impl<'a> Iterator for Iter<'a> {
type Item = CharPair;
fn next(&mut self) -> Option<CharPair> {
match (self.iter.next(), self.iter.next()) {
(Some(first), Some(last)) => Some((first, last)),
(None, None) => None,
_ => unreachable!(),
}
}
}
#[derive(Debug)]
pub struct IntoIter {
iter: OwnedChars,
}
impl<'a> IntoIterator for HexByteString {
type Item = CharPair;
type IntoIter = IntoIter;
fn into_iter(self) -> IntoIter {
IntoIter { iter: self.0.into_chars() }
}
}
impl Iterator for IntoIter {
type Item = CharPair;
fn next(&mut self) -> Option<CharPair> {
match (self.iter.next(), self.iter.next()) {
(Some(first), Some(last)) => Some((first, last)),
(None, None) => None,
_ => unreachable!(),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment