Open nick42d opened 3 months ago
Hi there @Mithronn, I'm taking a look at this currently. Be keen to get your opinion on a problem I'm having.
How critical is the content_length() method and where do you think it belongs? I can see the following options:
video.content_length().await
followed by video.stream().await
would calculate it twice. If video.content_length() took a mutable reference, we could cache the value however.With a bit more playing around, this could be an option too for the existing Stream trait. This is not my recommendation, as:
video.stream().await.into_futures_stream()
fn into_futures_stream(self) -> impl futures::Stream<Item = Result<Bytes, VideoError>>
where
Self: Sized,
{
// Second value of initialisation tuple represents if the previous iteration of
// the stream errored. If so, stream will close, as no future iterations of
// the stream are expected to return Ok.
futures::stream::unfold((self, false), |(state, err)| async move {
if err {
return None;
};
let chunk = state.chunk().await;
match chunk {
// Return error value on this iteration, on the next iteration return None.
Err(e) => Some((Err(e), (state, true))),
// Happy path
Ok(Some(bytes)) => Some((Ok(bytes), (state, false))),
// Stream has closed.
Ok(None) => None,
}
})
}
Hi there, my main use case is a music player -
youtui
. I am currently using therusty_ytdl::Stream
trait to be able to provide progress updates whilst downloads are in progress. However I thought it could be nice to implement thefutures::Steam
trait to enable the use of the adaptors infutures::StreamExt
. Example below of a pattern that this could enable.