How to read from Stream trait

I have the following type:

pub type MessageStream = Pin<Box<dyn Stream<Item = Result<rtsp_types::Message<Body>, ReadError>> + Send>>;

and a struct member:

stream: Option<MessageStream>,

That I call like this:

self.stream.as_mut().unwrap().poll_next();

but I get

116 |         let response = self.stream.as_mut().unwrap().poll_next();
    |                                                      ^^^^^^^^^ method not found in `&mut Pin<Box<(dyn futures::Stream<Item = std::result::Result<rtsp_types::Message<Body>, message_socket::ReadError>> + std::marker::Send + 'static)>>`

On futures::stream::Stream - Rust it lists only poll_next, but into_future() worked for me for some reason.

It's nice to convert to a future but I also want to poll_next, I'm trying lots of things.

What is wrong?

By the way, the Stream docs you linked are for futures 0.2.0, which is outdated now - here are the docs for 0.3.

Notice how poll_next takes Pin<&mut Self> - so even if Stream was implemented for that stream type, you only have an &mut and not a Pin<&mut>. What you can do is call Pin::as_mut, which will convert &mut Pin<Box<...>> to Pin<&mut ...>.

However, you shouldn't have to call poll_next (or any poll_* method) from async code - it's more low-level than that. Instead, you should be calling next which works from async. There are several nexts available, depending on what you're using:

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.