Skip to content

Iterator #229

@simonsan

Description

@simonsan
Collaborator

Tracking issue for merging: https://github.com/lpxxn/rust-design-pattern/blob/master/behavioral/iterator.rs

Example:

//! Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

trait Iterator<T> {
    fn next(&mut self) -> Option<T>;
    fn current(&self) -> Option<T>;
    fn has_next(&self) -> bool;
    fn reset(&mut self);
}

struct Container<T> {
    data: Vec<T>,
}

struct ConcreteIterator<'a, T> {
    idx: usize,
    container: &'a Container<T>,
}

impl<'a, T: Clone> ConcreteIterator<'a, T> {
    fn new(container: &'a Container<T>) -> ConcreteIterator<T> {
        ConcreteIterator { idx: 0, container }
    }
}

impl<'a, T: Clone> Iterator<T> for ConcreteIterator<'a, T> {
    fn next(&mut self) -> Option<T> {
        let item = self.container.data.get(self.idx).cloned();
        self.idx += 1;
        item
    }
    fn current(&self) -> Option<T> {
        self.container.data.get(self.idx).cloned()
    }
    fn has_next(&self) -> bool {
        self.container.data.len() > self.idx
    }
    fn reset(&mut self) {
        self.idx = 0;
    }
}

impl<T: Clone> Container<T> {
    fn new() -> Container<T> {
        Container { data: Vec::new() }
    }
    fn add_item(&mut self, item: T) {
        self.data.push(item);
    }
    fn iter(&self) -> impl Iterator<T> + '_ {
        ConcreteIterator::new(self)
    }
}

fn main() {
    let mut c = Container::new();
    c.add_item(1);
    c.add_item(2);
    c.add_item(3);

    let mut iter = c.iter();
    let has_next = iter.has_next();
    assert_eq!(has_next, true);
    let item = iter.next();
    println!("item: {:?}", item);
    iter.reset();
    while iter.has_next() {
        let v = iter.next().unwrap();
        println!("item: {}", v);
    }
    let item = iter.next();
    assert_eq!(item, None);
}

Activity

added
C-additionCategory: Adding new content, something that didn't exist in the repository before
A-patternArea: Content about Patterns
on Feb 22, 2021
simonsan

simonsan commented on Feb 22, 2021

@simonsan
CollaboratorAuthor

Tracking discussion on merging: lpxxn/rust-design-pattern#7

lpxxn

lpxxn commented on Feb 23, 2021

@lpxxn

i relicense to MPL-2.0,you can use and change my code.

added
C-needs discussionArea: Something that is not clear to everyone if it fixes something/adds valuable content
on Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternArea: Content about PatternsC-additionCategory: Adding new content, something that didn't exist in the repository beforeC-needs discussionArea: Something that is not clear to everyone if it fixes something/adds valuable content

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lpxxn@simonsan

        Issue actions

          Iterator · Issue #229 · rust-unofficial/patterns