flume icon indicating copy to clipboard operation
flume copied to clipboard

Allow using `into_stream()` with non-`'static` items

Open Seeker14491 opened this issue 3 years ago • 2 comments

Currently, the Receiver::into_stream() method is not usable if the type being sent through the channel is not 'static.

fn main() {
    let data = String::from("hello");
    let (tx, rx) = flume::unbounded();
    tx.send(&data).unwrap();
    let _stream = rx.into_stream();
}

Output:

error[E0597]: `data` does not live long enough
 --> src/main.rs:4:13
  |
4 |     tx.send(&data).unwrap();
  |             ^^^^^ borrowed value does not live long enough
5 |     let _stream = rx.into_stream();
  |                   ---------------- argument requires that `data` is borrowed for `'static`
6 | }
  | - `data` dropped here while still borrowed

This is the current function signature for into_stream():

fn into_stream(self) -> RecvStream<'static, T>

If we change it to this...

fn into_stream<'a>(self) -> RecvStream<'a, T>

...the above code compiles.

I ran into this issue when switching from futures-channel, as the channels from that crate do not have this limitation.

Seeker14491 avatar Oct 14 '22 06:10 Seeker14491

I think that change should work. We can also change the lifetime param on into_send_async and other functions to 'a.

Restioson avatar Oct 14 '22 08:10 Restioson

Sounds good to me. Any chance you could open a PR?

zesterer avatar Oct 14 '22 10:10 zesterer

Added a PR for this. Let me know what else I need to do

ericmcbride avatar Jan 31 '23 22:01 ericmcbride

Ideal, thanks! I believe this can be closed now.

zesterer avatar Feb 01 '23 20:02 zesterer