1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! We need to impl PartialEq, Eq, PartialOrd, Ord, and Hash for all handle types in wgpu.
//!
//! For types that have some already-unique property, we can use that property to implement these traits.
//!
//! For types (like WebGPU) that don't have such a property, we generate an identifier and use that.

use std::{
    num::NonZeroU64,
    sync::atomic::{AtomicU64, Ordering},
};

static NEXT_ID: AtomicU64 = AtomicU64::new(1);

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Identifier {
    inner: NonZeroU64,
}

impl Identifier {
    pub fn create() -> Self {
        let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
        // Safety: Will take 7000+ years of constant incrementing to overflow. It's fine.
        let inner = unsafe { NonZeroU64::new_unchecked(id) };
        Self { inner }
    }
}

/// Implements PartialEq, Eq, PartialOrd, Ord, and Hash for a type by proxying the operations to a single field.
///
/// ```ignore
/// impl_eq_ord_hash_proxy!(MyType => .field);
/// ```
macro_rules! impl_eq_ord_hash_proxy {
    ($type:ty => $($access:tt)*) => {
        impl PartialEq for $type {
            fn eq(&self, other: &Self) -> bool {
                self $($access)* == other $($access)*
            }
        }

        impl Eq for $type {}

        impl PartialOrd for $type {
            fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }

        impl Ord for $type {
            fn cmp(&self, other: &Self) -> std::cmp::Ordering {
                self $($access)*.cmp(&other $($access)*)
            }
        }

        impl std::hash::Hash for $type {
            fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
                self $($access)*.hash(state)
            }
        }
    };
}

/// Implements PartialEq, Eq, PartialOrd, Ord, and Hash for a type by comparing the addresses of the Arcs.
///
/// ```ignore
/// impl_eq_ord_hash_arc_address!(MyType => .field);
/// ```
#[cfg_attr(not(wgpu_core), allow(unused_macros))]
macro_rules! impl_eq_ord_hash_arc_address {
    ($type:ty => $($access:tt)*) => {
        impl PartialEq for $type {
            fn eq(&self, other: &Self) -> bool {
                let address_left = std::sync::Arc::as_ptr(&self $($access)*);
                let address_right = std::sync::Arc::as_ptr(&other $($access)*);

                address_left == address_right
            }
        }

        impl Eq for $type {}

        impl PartialOrd for $type {
            fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }

        impl Ord for $type {
            fn cmp(&self, other: &Self) -> std::cmp::Ordering {
                let address_left = std::sync::Arc::as_ptr(&self $($access)*);
                let address_right = std::sync::Arc::as_ptr(&other $($access)*);

                address_left.cmp(&address_right)
            }
        }

        impl std::hash::Hash for $type {
            fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
                let address = std::sync::Arc::as_ptr(&self $($access)*);
                address.hash(state)
            }
        }
    };
}

#[cfg_attr(not(wgpu_core), allow(unused_imports))]
pub(crate) use {impl_eq_ord_hash_arc_address, impl_eq_ord_hash_proxy};