Skip to content

Commit

Permalink
Simplify async map API
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jul 8, 2024
1 parent bdc6179 commit 62f2aff
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
5 changes: 2 additions & 3 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,10 @@ impl Context {
CopyBuffer::new(&self.0, size)
}

pub async fn map_view<'a, S, R, F>(&self, view: CopyBufferView<'a>, tx: S, rx: R) -> Mapped<'a>
pub async fn map_view<'a, S, R>(&self, view: CopyBufferView<'a>, tx: S, rx: R) -> Mapped<'a>
where
S: FnOnce(MapResult) + wgpu::WasmNotSend + 'static,
R: FnOnce() -> F,
F: IntoFuture<Output = MapResult>,
R: IntoFuture<Output = MapResult>,
{
view.map(&self.0, tx, rx).await
}
Expand Down
7 changes: 3 additions & 4 deletions dunge/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,16 @@ pub type MapResult = Result<(), BufferAsyncError>;
pub struct CopyBufferView<'a>(BufferSlice<'a>);

impl<'a> CopyBufferView<'a> {
pub(crate) async fn map<S, R, F>(self, state: &State, tx: S, rx: R) -> Mapped<'a>
pub(crate) async fn map<S, R>(self, state: &State, tx: S, rx: R) -> Mapped<'a>
where
S: FnOnce(MapResult) + WasmNotSend + 'static,
R: FnOnce() -> F,
F: IntoFuture<Output = MapResult>,
R: IntoFuture<Output = MapResult>,
{
use wgpu::*;

self.0.map_async(MapMode::Read, tx);
state.device().poll(Maintain::Wait);
if let Err(err) = rx().await {
if let Err(err) = rx.await {
panic!("failed to copy texture: {err}");
}

Expand Down
4 changes: 2 additions & 2 deletions helpers/src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{future::Future, pin::Pin};

type Sender<T> = Box<dyn FnOnce(T) + Send>;
type Receiver<T> = Box<dyn FnOnce() -> Pin<Box<dyn Future<Output = T>>>>;
type Receiver<T> = Pin<Box<dyn Future<Output = T>>>;

pub fn oneshot<T>() -> (Sender<T>, Receiver<T>)
where
Expand All @@ -10,6 +10,6 @@ where
let (tx, rx) = async_channel::bounded(1);
(
Box::new(move |r| tx.send_blocking(r).expect("send mapped result")),
Box::new(|| Box::pin(async move { rx.recv().await.expect("recv mapped result") })),
Box::pin(async move { rx.recv().await.expect("recv mapped result") }),
)
}

0 comments on commit 62f2aff

Please sign in to comment.