Skip to content

Commit

Permalink
implement PartialEq, Eq, Hash, PartialOrd and Ord for wgpu …
Browse files Browse the repository at this point in the history
…resources
  • Loading branch information
teoxoy committed Aug 21, 2024
1 parent a40cbf6 commit 242d2a8
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/bind_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/bind_group_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_> {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/compute_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions wgpu/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: std::hash::Hasher>(&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<std::cmp::Ordering> {
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;
2 changes: 2 additions & 0 deletions wgpu/src/api/pipeline_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/query_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/render_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/render_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/shader_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/api/texture_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 242d2a8

Please sign in to comment.