use ManuallyDrop for Destroyed{Buffer,Texture}

This commit is contained in:
teoxoy 2024-07-12 15:10:39 +02:00 committed by Teodor Tanasoaia
parent 650054bbcd
commit 76f9b2f87a

View File

@ -711,7 +711,7 @@ impl<A: HalApi> Buffer<A> {
};
queue::TempResource::DestroyedBuffer(DestroyedBuffer {
raw: Some(raw),
raw: ManuallyDrop::new(raw),
device: Arc::clone(&self.device),
label: self.label().to_owned(),
bind_groups,
@ -761,7 +761,7 @@ crate::impl_trackable!(Buffer);
/// A buffer that has been marked as destroyed and is staged for actual deletion soon.
#[derive(Debug)]
pub struct DestroyedBuffer<A: HalApi> {
raw: Option<A::Buffer>,
raw: ManuallyDrop<A::Buffer>,
device: Arc<Device<A>>,
label: String,
bind_groups: Vec<Weak<BindGroup<A>>>,
@ -781,13 +781,12 @@ impl<A: HalApi> Drop for DestroyedBuffer<A> {
}
drop(deferred);
if let Some(raw) = self.raw.take() {
resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label());
unsafe {
use hal::Device;
self.device.raw().destroy_buffer(raw);
}
resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label());
// SAFETY: We are in the Drop impl and we don't use self.raw anymore after this point.
let raw = unsafe { ManuallyDrop::take(&mut self.raw) };
unsafe {
use hal::Device;
self.device.raw().destroy_buffer(raw);
}
}
}
@ -1174,7 +1173,7 @@ impl<A: HalApi> Texture<A> {
};
queue::TempResource::DestroyedTexture(DestroyedTexture {
raw: Some(raw),
raw: ManuallyDrop::new(raw),
views,
bind_groups,
device: Arc::clone(&self.device),
@ -1363,7 +1362,7 @@ impl Global {
/// A texture that has been marked as destroyed and is staged for actual deletion soon.
#[derive(Debug)]
pub struct DestroyedTexture<A: HalApi> {
raw: Option<A::Texture>,
raw: ManuallyDrop<A::Texture>,
views: Vec<Weak<TextureView<A>>>,
bind_groups: Vec<Weak<BindGroup<A>>>,
device: Arc<Device<A>>,
@ -1389,13 +1388,12 @@ impl<A: HalApi> Drop for DestroyedTexture<A> {
}
drop(deferred);
if let Some(raw) = self.raw.take() {
resource_log!("Destroy raw Texture (destroyed) {:?}", self.label());
unsafe {
use hal::Device;
self.device.raw().destroy_texture(raw);
}
resource_log!("Destroy raw Texture (destroyed) {:?}", self.label());
// SAFETY: We are in the Drop impl and we don't use self.raw anymore after this point.
let raw = unsafe { ManuallyDrop::take(&mut self.raw) };
unsafe {
use hal::Device;
self.device.raw().destroy_texture(raw);
}
}
}