From 86e12499499f19b2b1439ad734bb9bfecca7e9d7 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Sat, 9 Mar 2019 13:56:32 -0500 Subject: [PATCH] Add the static lifetime bound to async buffer mapping callbacks These callbacks are executed outside of the mapping function and could previously reference data that had been dropped. This fixes the soundness issues described in #95, but not the buggy mapping behavior. --- examples/hello_compute_rust/main.rs | 5 ++++- wgpu-rs/src/lib.rs | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/hello_compute_rust/main.rs b/examples/hello_compute_rust/main.rs index ef5bdef87..27c5ad774 100644 --- a/examples/hello_compute_rust/main.rs +++ b/examples/hello_compute_rust/main.rs @@ -88,13 +88,16 @@ fn main() { } encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size); + device.get_queue().submit(&[encoder.finish()]); + staging_buffer.map_read_async(0, size, |result: wgpu::BufferMapAsyncResult<&[u32]>| { if let wgpu::BufferMapAsyncResult::Success(data) = result { println!("Times: {:?}", data); } - staging_buffer.unmap(); + // staging_buffer.unmap(); // TODO }); + let encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); device.get_queue().submit(&[encoder.finish()]); } diff --git a/wgpu-rs/src/lib.rs b/wgpu-rs/src/lib.rs index cd456656a..32eb32b7d 100644 --- a/wgpu-rs/src/lib.rs +++ b/wgpu-rs/src/lib.rs @@ -530,7 +530,7 @@ impl Buffer { pub fn map_read_async(&self, start: u32, size: u32, callback: F) where T: 'static + Copy, - F: FnOnce(BufferMapAsyncResult<&[T]>), + F: FnOnce(BufferMapAsyncResult<&[T]>) + 'static, { let type_size = std::mem::size_of::() as u32; assert_ne!(type_size, 0); @@ -575,7 +575,7 @@ impl Buffer { pub fn map_write_async(&self, start: u32, size: u32, callback: F) where T: 'static + Copy, - F: FnOnce(BufferMapAsyncResult<&mut [T]>), + F: FnOnce(BufferMapAsyncResult<&mut [T]>) + 'static, { let type_size = std::mem::size_of::() as u32; assert_ne!(type_size, 0);