54 lines
1.2 KiB
Rust
Raw Normal View History

2018-09-02 22:10:11 -05:00
//! Contains implementations of `InputIter`.
use std::fmt::Debug;
2018-09-03 00:06:15 -05:00
use std::iter::Iterator;
2018-09-02 22:10:11 -05:00
2018-09-03 00:06:15 -05:00
use super::{InputIter, Offsetable};
2018-09-02 22:10:11 -05:00
/// Implements `InputIter` for any slice of T.
#[derive(Debug)]
pub struct SliceIter<'a, T: Debug + 'a> {
source: &'a [T],
offset: usize,
}
impl<'a, T: Debug + 'a> SliceIter<'a, T> {
/// new constructs a SliceIter from a Slice of T.
pub fn new(source: &'a [T]) -> Self {
SliceIter {
source: source,
offset: 0,
}
}
}
impl<'a, T: Debug + 'a> Iterator for SliceIter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
match self.source.get(self.offset) {
Some(item) => {
self.offset += 1;
Some(item)
2018-09-03 00:06:15 -05:00
}
2018-09-02 22:10:11 -05:00
None => None,
}
}
}
impl<'a, T: Debug + 'a> Offsetable for SliceIter<'a, T> {
2018-09-02 22:10:11 -05:00
fn get_offset(&self) -> usize {
self.offset
}
}
impl<'a, T: Debug + 'a> Clone for SliceIter<'a, T> {
fn clone(&self) -> Self {
SliceIter {
source: self.source,
offset: self.offset,
}
}
}
2018-09-03 00:06:15 -05:00
impl<'a, T: Debug + 'a> InputIter for SliceIter<'a, T> {}