contain-rs / linked-hash-map

A HashMap wrapper that holds key-value pairs in insertion order
https://contain-rs.github.io/linked-hash-map/linked_hash_map/
Apache License 2.0
169 stars 60 forks source link

Consuming iterator #57

Closed dtolnay closed 8 years ago

dtolnay commented 8 years ago

Fixes #4.

FlashCat commented 8 years ago

r? @huonw

(I've picked a reviewer for you, use r? to override)

dtolnay commented 8 years ago

r? @apasel422

apasel422 commented 8 years ago

Looks good. It might be beneficial to have one more test that ensures that the every value is dropped exactly once:

#[test]
fn test_into_iter_drop() {
    struct Counter<'a>(&'a mut usize);

    impl<'a> Drop for Counter<'a> {
        fn drop(&mut self) {
            *self.0 += 1;
        }
    }

    let mut a = 0;
    let mut b = 0;
    let mut c = 0;

    {
        let mut map = LinkedHashMap::new();
        map.insert("a", Counter(&mut a));
        map.insert("b", Counter(&mut b));
        map.insert("c", Counter(&mut c));

        let mut iter = map.into_iter();
        iter.next();
        iter.next_back();
    }

    assert_eq!(a, 1);
    assert_eq!(b, 1);
    assert_eq!(c, 1);
}
dtolnay commented 8 years ago

Nice. I added it.