First, using return
in those positions is unidiomatic.
Anyway, the lifetime annotation on RwLockReadGuard<'a, Vec<Connection>>
is only an upper bound on how long the read guard can live. The guard is allowed to live shorter than that annotation. This means that you cannot produce a borrow into the insides of the guard annotated by that lifetime, since that borrow could then live longer than the guard, even if the cannot outlive 'a
, which would be unsound. Consider reading this. To get around this, your iterator would have to be a borrow of the read guard.
That said, does your Connection
type happen to contain IO primitives such as a TcpStream
? In that case please be aware that letting IO primitives be owned by a single task and using channels for communication is almost always better than putting it behind some kind of lock.