From 4c448c3fc583da6d22c62c7e92bbb74b53863357 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 5 May 2020 21:52:06 -0400 Subject: [PATCH] Add a way to destroy a pass by a mutable reference (#647) --- wgpu-core/src/command/mod.rs | 16 ++++++++++++++-- wgpu-core/src/command/render.rs | 3 +-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index d2c15ec73..63a3b61e9 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -93,7 +93,16 @@ impl RawPass { self.data as usize - self.base as usize } - pub unsafe fn into_vec(self) -> (Vec, id::CommandEncoderId) { + /// Recover the data vector of the pass, consuming `self`. + unsafe fn into_vec(mut self) -> (Vec, id::CommandEncoderId) { + (self.invalidate(), self.parent) + } + + /// Make pass contents invalid, return the contained data. + /// + /// Any following access to the pass will result in a crash + /// for accessing address 0. + pub unsafe fn invalidate(&mut self) -> Vec { let size = self.size(); assert!( size <= self.capacity, @@ -102,7 +111,10 @@ impl RawPass { self.capacity ); let vec = Vec::from_raw_parts(self.base, size, self.capacity); - (vec, self.parent) + self.data = ptr::null_mut(); + self.base = ptr::null_mut(); + self.capacity = 0; + vec } unsafe fn ensure_extra_size(&mut self, extra_size: usize) { diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 0cc592030..34815f2d1 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -160,8 +160,7 @@ impl super::RawPass { pub unsafe fn finish_render(mut self) -> (Vec, id::CommandEncoderId) { self.finish(RenderCommand::End); - let (vec, parent_id) = self.into_vec(); - (vec, parent_id) + self.into_vec() } }