tokio-rs/slab

iterate over the slab

NickeZ opened this issue · 2 comments

Hey,

I guess I'm just stupid or aren't using the slab correctly but I fail to understand how to iterate (key, value) over the slab.

One solution I see is that I could put the key inside the value but it wouldn't be ergonomic. I would have to have a Option<Token> set to None initially in the value struct and then have a setter to set it after it is added to the slab.

let token = clients.insert(client)
client.set_token(token)
for client in clients.iter() {
    poll.reregister(client.stream, client.token, ...)

compared to:

let token = clients.insert(client)
for (tok, client) in clients.iter() {
    poll.reregister(client.stream, tok, ...)

The hashmap returns both key and value:

HashMap.iter().next() -> Option<(&'a K, &'a V)>

whereas the slab only returns the values:

Slab.iter().next() -> Option<&'a T>

btw, would you accept a pull request that changed the next() behavior or added some new function?

Slab offers an iterator on master.