LPGhatguy / thunderdome

Arena type inspired by generational-arena
Apache License 2.0
197 stars 15 forks source link

impl IntoIterator for {&,&mut}Arena #18

Closed toyboot4e closed 3 years ago

toyboot4e commented 3 years ago

Currently, Arena::iter or Arena::iter_mut have to be called to use iterators. This PR adds trait impls that allow idiomatic loop syntax: for .. in &arena.

Example:

use thunderdome::Arena;

fn main() {
    let mut arena = Arena::new();
    arena.insert(1 as usize);
    arena.insert(2);
    arena.insert(3);
    for (index, item) in &arena {
        println!("{:?}, {:?}", index, item);
    }
}

Output:

Index { slot: 0, generation: Generation(1) }, 1
Index { slot: 1, generation: Generation(1) }, 2
Index { slot: 2, generation: Generation(1) }, 3

The iterators return (index, item) pairs, but it's not common, so item-only iterator can be preferable.

What would you think? Thank you.

LPGhatguy commented 3 years ago

These iterators look perfect to me! Can you also add an entry to CHANGELOG.md to mention that these iterators were introduced?

toyboot4e commented 3 years ago

Yes, I commited now :)

Edit: Feel free to modify my PR. I'm not sure if I did it right, actually.

LPGhatguy commented 3 years ago

Thank you!