Skip to content

Commit

Permalink
Add a way to destroy a pass by a mutable reference (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark authored May 6, 2020
1 parent e2d713a commit 4c448c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 14 additions & 2 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ impl RawPass {
self.data as usize - self.base as usize
}

pub unsafe fn into_vec(self) -> (Vec<u8>, id::CommandEncoderId) {
/// Recover the data vector of the pass, consuming `self`.
unsafe fn into_vec(mut self) -> (Vec<u8>, 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<u8> {
let size = self.size();
assert!(
size <= self.capacity,
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ impl super::RawPass {

pub unsafe fn finish_render(mut self) -> (Vec<u8>, id::CommandEncoderId) {
self.finish(RenderCommand::End);
let (vec, parent_id) = self.into_vec();
(vec, parent_id)
self.into_vec()
}
}

Expand Down

0 comments on commit 4c448c3

Please sign in to comment.