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

Iterator #229

Open
Tracked by #116
simonsan opened this issue Feb 22, 2021 · 2 comments
Open
Tracked by #116

Iterator #229

simonsan opened this issue Feb 22, 2021 · 2 comments
Labels
A-pattern Area: Content about Patterns C-addition Category: Adding new content, something that didn't exist in the repository before C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content

Comments

@simonsan
Copy link
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);
}
@simonsan simonsan added C-addition Category: Adding new content, something that didn't exist in the repository before A-pattern Area: Content about Patterns labels Feb 22, 2021
@simonsan
Copy link
Collaborator Author

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

@lpxxn
Copy link

lpxxn commented Feb 23, 2021

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

@simonsan simonsan added the C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content label Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pattern Area: Content about Patterns C-addition Category: Adding new content, something that didn't exist in the repository before C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content
Projects
None yet
Development

No branches or pull requests

2 participants