Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsyncSeekForwardExt trait to allows a similar API to the previous Bevy version #16027

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/bevy_asset/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -120,6 +121,40 @@ impl<T: ?Sized + AsyncSeekForward + Unpin> AsyncSeekForward for Box<T> {
}
}

/// 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>
cart marked this conversation as resolved.
Show resolved Hide resolved
where
Self: Unpin,
{
SeekForwardFuture {
seeker: self,
offset,
}
}
}

impl<R: AsyncSeekForward + ?Sized> 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<S: Unpin + ?Sized> Unpin for SeekForwardFuture<'_, S> {}

impl<S: AsyncSeekForward + Unpin + ?Sized> Future for SeekForwardFuture<'_, S> {
type Output = futures_lite::io::Result<u64>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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.
///
Expand Down