diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b4d18f65ca..dfeeb088925 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ By @wumpf in [#6069](https://github.com/gfx-rs/wgpu/pull/6069), [#6099](https:// - Reduce the amount of debug and trace logs emitted by wgpu-core and wgpu-hal. By @nical in [#6065](https://github.com/gfx-rs/wgpu/issues/6065) - `Rg11b10Float` is renamed to `Rg11b10UFloat`. By @sagudev in [#6108](https://github.com/gfx-rs/wgpu/pull/6108) - Removed `.global_id()` getters from wgpu objects. By @teoxoy [#6134](https://github.com/gfx-rs/wgpu/pull/6134) +- Implement `PartialEq`, `Eq`, `Hash`, `PartialOrd` and `Ord` for wgpu resources. By @teoxoy [#6134](https://github.com/gfx-rs/wgpu/pull/6134) ### Dependency Updates diff --git a/wgpu/src/api/bind_group.rs b/wgpu/src/api/bind_group.rs index 6b4add61715..42a774b2952 100644 --- a/wgpu/src/api/bind_group.rs +++ b/wgpu/src/api/bind_group.rs @@ -18,6 +18,8 @@ pub struct BindGroup { #[cfg(send_sync)] static_assertions::assert_impl_all!(BindGroup: Send, Sync); +super::impl_partialeq_eq_hash!(BindGroup); + impl Drop for BindGroup { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/bind_group_layout.rs b/wgpu/src/api/bind_group_layout.rs index 2a059deacd8..db335689ca1 100644 --- a/wgpu/src/api/bind_group_layout.rs +++ b/wgpu/src/api/bind_group_layout.rs @@ -21,6 +21,8 @@ pub struct BindGroupLayout { #[cfg(send_sync)] static_assertions::assert_impl_all!(BindGroupLayout: Send, Sync); +super::impl_partialeq_eq_hash!(BindGroupLayout); + impl Drop for BindGroupLayout { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/buffer.rs b/wgpu/src/api/buffer.rs index 3c25f1c3c48..d5687a78dc3 100644 --- a/wgpu/src/api/buffer.rs +++ b/wgpu/src/api/buffer.rs @@ -182,6 +182,8 @@ pub struct Buffer { #[cfg(send_sync)] static_assertions::assert_impl_all!(Buffer: Send, Sync); +super::impl_partialeq_eq_hash!(Buffer); + impl Buffer { /// Return the binding view of the entire buffer. pub fn as_entire_binding(&self) -> BindingResource<'_> { diff --git a/wgpu/src/api/compute_pipeline.rs b/wgpu/src/api/compute_pipeline.rs index b5d48e7206d..50f17122ea8 100644 --- a/wgpu/src/api/compute_pipeline.rs +++ b/wgpu/src/api/compute_pipeline.rs @@ -16,6 +16,8 @@ pub struct ComputePipeline { #[cfg(send_sync)] static_assertions::assert_impl_all!(ComputePipeline: Send, Sync); +super::impl_partialeq_eq_hash!(ComputePipeline); + impl ComputePipeline { /// Get an object representing the bind group layout at a given index. pub fn get_bind_group_layout(&self, index: u32) -> BindGroupLayout { diff --git a/wgpu/src/api/mod.rs b/wgpu/src/api/mod.rs index 0b56c5d69f4..52b9ec1602d 100644 --- a/wgpu/src/api/mod.rs +++ b/wgpu/src/api/mod.rs @@ -76,3 +76,35 @@ pub use texture_view::*; /// Object debugging label. pub type Label<'a> = Option<&'a str>; + +macro_rules! impl_partialeq_eq_hash { + ($ty:ty) => { + impl PartialEq for $ty { + fn eq(&self, other: &Self) -> bool { + std::ptr::addr_eq(self.data.as_ref(), other.data.as_ref()) + } + } + impl Eq for $ty {} + + impl std::hash::Hash for $ty { + fn hash(&self, state: &mut H) { + let ptr = self.data.as_ref() as *const Data as *const (); + ptr.hash(state); + } + } + + impl PartialOrd for $ty { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } + } + impl Ord for $ty { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + let a = self.data.as_ref() as *const Data as *const (); + let b = other.data.as_ref() as *const Data as *const (); + a.cmp(&b) + } + } + }; +} +pub(crate) use impl_partialeq_eq_hash; diff --git a/wgpu/src/api/pipeline_layout.rs b/wgpu/src/api/pipeline_layout.rs index f8d987bfb07..2b89d2b7aa1 100644 --- a/wgpu/src/api/pipeline_layout.rs +++ b/wgpu/src/api/pipeline_layout.rs @@ -16,6 +16,8 @@ pub struct PipelineLayout { #[cfg(send_sync)] static_assertions::assert_impl_all!(PipelineLayout: Send, Sync); +super::impl_partialeq_eq_hash!(PipelineLayout); + impl Drop for PipelineLayout { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/query_set.rs b/wgpu/src/api/query_set.rs index 5609c76b1f8..a0cac6847b5 100644 --- a/wgpu/src/api/query_set.rs +++ b/wgpu/src/api/query_set.rs @@ -16,6 +16,8 @@ pub struct QuerySet { #[cfg(send_sync)] static_assertions::assert_impl_all!(QuerySet: Send, Sync); +super::impl_partialeq_eq_hash!(QuerySet); + impl Drop for QuerySet { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/render_bundle.rs b/wgpu/src/api/render_bundle.rs index ff449dc2cf1..5932458aebe 100644 --- a/wgpu/src/api/render_bundle.rs +++ b/wgpu/src/api/render_bundle.rs @@ -19,6 +19,8 @@ pub struct RenderBundle { #[cfg(send_sync)] static_assertions::assert_impl_all!(RenderBundle: Send, Sync); +super::impl_partialeq_eq_hash!(RenderBundle); + impl Drop for RenderBundle { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/render_pipeline.rs b/wgpu/src/api/render_pipeline.rs index c6ac9bdec93..1893f7c7b28 100644 --- a/wgpu/src/api/render_pipeline.rs +++ b/wgpu/src/api/render_pipeline.rs @@ -16,6 +16,8 @@ pub struct RenderPipeline { #[cfg(send_sync)] static_assertions::assert_impl_all!(RenderPipeline: Send, Sync); +super::impl_partialeq_eq_hash!(RenderPipeline); + impl Drop for RenderPipeline { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/sampler.rs b/wgpu/src/api/sampler.rs index ea5dd364833..d60bcccd267 100644 --- a/wgpu/src/api/sampler.rs +++ b/wgpu/src/api/sampler.rs @@ -19,6 +19,8 @@ pub struct Sampler { #[cfg(send_sync)] static_assertions::assert_impl_all!(Sampler: Send, Sync); +super::impl_partialeq_eq_hash!(Sampler); + impl Drop for Sampler { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/shader_module.rs b/wgpu/src/api/shader_module.rs index 37fedaa36a0..20334a75ade 100644 --- a/wgpu/src/api/shader_module.rs +++ b/wgpu/src/api/shader_module.rs @@ -18,6 +18,8 @@ pub struct ShaderModule { #[cfg(send_sync)] static_assertions::assert_impl_all!(ShaderModule: Send, Sync); +super::impl_partialeq_eq_hash!(ShaderModule); + impl Drop for ShaderModule { fn drop(&mut self) { if !thread::panicking() { diff --git a/wgpu/src/api/texture.rs b/wgpu/src/api/texture.rs index 793a522c741..9f4f6ad4cf6 100644 --- a/wgpu/src/api/texture.rs +++ b/wgpu/src/api/texture.rs @@ -18,6 +18,8 @@ pub struct Texture { #[cfg(send_sync)] static_assertions::assert_impl_all!(Texture: Send, Sync); +super::impl_partialeq_eq_hash!(Texture); + impl Texture { /// Returns the inner hal Texture using a callback. The hal texture will be `None` if the /// backend type argument does not match with this wgpu Texture diff --git a/wgpu/src/api/texture_view.rs b/wgpu/src/api/texture_view.rs index d610d6f36f6..bba82c745bf 100644 --- a/wgpu/src/api/texture_view.rs +++ b/wgpu/src/api/texture_view.rs @@ -16,6 +16,8 @@ pub struct TextureView { #[cfg(send_sync)] static_assertions::assert_impl_all!(TextureView: Send, Sync); +super::impl_partialeq_eq_hash!(TextureView); + impl TextureView { /// Returns the inner hal TextureView using a callback. The hal texture will be `None` if the /// backend type argument does not match with this wgpu Texture