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.
This commit is contained in:
Aaron Loucks 2019-03-09 13:56:32 -05:00
parent 0bb4daa696
commit 86e1249949
2 changed files with 6 additions and 3 deletions

View File

@ -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()]);
}

View File

@ -530,7 +530,7 @@ impl Buffer {
pub fn map_read_async<T, F>(&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::<T>() as u32;
assert_ne!(type_size, 0);
@ -575,7 +575,7 @@ impl Buffer {
pub fn map_write_async<T, F>(&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::<T>() as u32;
assert_ne!(type_size, 0);