Confusions in tokio's AsyncRead

Hi,
Given the following code: Rust Playground

I can spawn reading from File, but not for &[u8].
Two things I don't understand:

  1. How come File in 'test_file' satisfies 'static lifetime requirement?
  2. How can I call read on Vec (commented out test_buffer function)? I can pass ownership to transform() (i.e. I don't need it). Is the "solution" at the bottom the best option (admittedly not tested)?

Thank you!

Because it doesn't borrow anything. T: 'static as a type bound means "you are allowed to keep it alive forever" (i.e., it doesn't contain temporary references, at most only 'static ones). Maybe your misunderstanding is that you expect it to mean "it necessarily lives forever", which it isn't.

You don't need to create a new wrapper type. The snippet compiles if you wrap the vector in an std::io::Cursor.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.