zesterer / flume

A safe and fast multi-producer, multi-consumer channel.
https://crates.io/crates/flume
Apache License 2.0
2.47k stars 85 forks source link

Remove lifetime constraint on Reciever.drain() #113

Closed wyatt-herkamp closed 2 years ago

wyatt-herkamp commented 2 years ago
    fn handle_updates(&mut self) {
        for update in self.server_update_receiver.drain() {
            self.handle_update(update);
        }
    }

So I have this function in my structure that is going to handle all updates received in the channel. Because the fact Drain has the life time of a immutable borrow for self. I now have an immutable borrow on self. Meaning I can't leave this function

    /// A phantom field used to constrain the lifetime of this iterator. We do this because the
    /// implementation may change and we don't want to unintentionally constrain it. Removing this
    /// lifetime later is a possibility.

I noticed the docs said this and I was wondering if you were considering removing the life time constraint. Anytime soon because it would make my code a bit easier if I don't have all of the update handling done within this one method.

zesterer commented 2 years ago

I think the lifetime is still desirable because even though it's circumstantially not needed due to the design of the crate, there's still a logical relationship here.

In your case, you could just clone the receiver (cloning senders and receivers is fairly cheap) like so:

    fn handle_updates(&mut self) {
        let r = self.server_update_receiver.clone();
        for update in r.drain() {
            self.handle_update(update);
        }
    }
wyatt-herkamp commented 2 years ago

Ok