diff --git a/crates/bevy_asset/src/io/mod.rs b/crates/bevy_asset/src/io/mod.rs index 948382ca31..eceefb2e7a 100644 --- a/crates/bevy_asset/src/io/mod.rs +++ b/crates/bevy_asset/src/io/mod.rs @@ -23,6 +23,7 @@ pub use source::*; use alloc::sync::Arc; use bevy_utils::{BoxedFuture, ConditionalSendFuture}; +use core::future::Future; use core::{ mem::size_of, pin::Pin, @@ -120,6 +121,40 @@ impl AsyncSeekForward for Box { } } +/// Extension trait for [`AsyncSeekForward`]. +pub trait AsyncSeekForwardExt: AsyncSeekForward { + /// Seek by the provided `offset` in the forwards direction, using the [`AsyncSeekForward`] trait. + fn seek_forward(&mut self, offset: u64) -> SeekForwardFuture<'_, Self> + where + Self: Unpin, + { + SeekForwardFuture { + seeker: self, + offset, + } + } +} + +impl AsyncSeekForwardExt for R {} + +#[derive(Debug)] +#[must_use = "futures do nothing unless you `.await` or poll them"] +pub struct SeekForwardFuture<'a, S: Unpin + ?Sized> { + seeker: &'a mut S, + offset: u64, +} + +impl Unpin for SeekForwardFuture<'_, S> {} + +impl Future for SeekForwardFuture<'_, S> { + type Output = futures_lite::io::Result; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let offset = self.offset; + Pin::new(&mut *self.seeker).poll_seek_forward(cx, offset) + } +} + /// A type returned from [`AssetReader::read`], which is used to read the contents of a file /// (or virtual file) corresponding to an asset. ///