lucaspoffo / renet

Server/Client network library for multiplayer games with authentication and connection management made with Rust
Apache License 2.0
663 stars 68 forks source link

Return an iterator from clients_id #47

Closed Shatur closed 1 year ago

Shatur commented 2 years ago

Currently RenetServer::clients_id() returns Vec but it's not very flexible and users usually wants to iterate over them in most cases. So I would suggest to return impl Iterator instead.

lucaspoffo commented 1 year ago

The problem with returning an iterator is that it would keep a reference to RenetServer, so we would not be able to call:

// This would need to be server.clients_id().collect().into_iter() {
for client_id in server.clients_id() {
   while let Some(message) = server.receive_message(client_id, channel_id) {
       // ...
   }
}

I could return an owned iterator by creating a new Vec and returning an iterator over it, but at this point, why not just return the Vec. Could also implement a new function just for the iterator I guess.

There might be a better way, not sure

Shatur commented 1 year ago

Right, it won't work because of the borrow checker :( Better keep as is then.

Shatur commented 1 year ago

Could also implement a new function just for the iterator I guess.

Although, having iter_clients_id() for read-only cases would be nice.

lucaspoffo commented 1 year ago

Added in #91