Created
May 31, 2017 22:18
-
-
Save U007D/2832c085865bb73d2bffe7d1bc9d9b08 to your computer and use it in GitHub Desktop.
Consuming String Iterator Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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