Skip to content

Commit ca729cc

Browse files
teoxoyjimblandy
authored andcommitted
Introduce HashableLiteral
The only reason `Literal` had to implement `Eq` and `Hash` was due to the SPV backend having to cache those in a `HashMap`. The `PartialEq` impl was error prone due to the match not being exhaustive.
1 parent d08a069 commit ca729cc

File tree

4 files changed

+27
-52
lines changed

4 files changed

+27
-52
lines changed

naga/src/back/spv/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl recyclable::Recyclable for CachedExpressions {
457457

458458
#[derive(Eq, Hash, PartialEq)]
459459
enum CachedConstant {
460-
Literal(crate::Literal),
460+
Literal(crate::proc::HashableLiteral),
461461
Composite {
462462
ty: LookupType,
463463
constituent_ids: Vec<Word>,

naga/src/back/spv/writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ impl Writer {
11501150
}
11511151

11521152
pub(super) fn get_constant_scalar(&mut self, value: crate::Literal) -> Word {
1153-
let scalar = CachedConstant::Literal(value);
1153+
let scalar = CachedConstant::Literal(value.into());
11541154
if let Some(&id) = self.cached_constants.get(&scalar) {
11551155
return id;
11561156
}

naga/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ pub enum TypeInner {
871871
BindingArray { base: Handle<Type>, size: ArraySize },
872872
}
873873

874-
#[derive(Debug, Clone, Copy, PartialOrd)]
874+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
875875
#[cfg_attr(feature = "serialize", derive(Serialize))]
876876
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
877877
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]

naga/src/proc/mod.rs

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -153,56 +153,31 @@ impl super::Scalar {
153153
}
154154
}
155155

156-
impl PartialEq for crate::Literal {
157-
fn eq(&self, other: &Self) -> bool {
158-
match (*self, *other) {
159-
(Self::F64(a), Self::F64(b)) => a.to_bits() == b.to_bits(),
160-
(Self::F32(a), Self::F32(b)) => a.to_bits() == b.to_bits(),
161-
(Self::U32(a), Self::U32(b)) => a == b,
162-
(Self::I32(a), Self::I32(b)) => a == b,
163-
(Self::U64(a), Self::U64(b)) => a == b,
164-
(Self::I64(a), Self::I64(b)) => a == b,
165-
(Self::Bool(a), Self::Bool(b)) => a == b,
166-
_ => false,
167-
}
168-
}
156+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
157+
pub enum HashableLiteral {
158+
F64(u64),
159+
F32(u32),
160+
U32(u32),
161+
I32(i32),
162+
U64(u64),
163+
I64(i64),
164+
Bool(bool),
165+
AbstractInt(i64),
166+
AbstractFloat(u64),
169167
}
170-
impl Eq for crate::Literal {}
171-
impl std::hash::Hash for crate::Literal {
172-
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
173-
match *self {
174-
Self::F64(v) | Self::AbstractFloat(v) => {
175-
hasher.write_u8(0);
176-
v.to_bits().hash(hasher);
177-
}
178-
Self::F32(v) => {
179-
hasher.write_u8(1);
180-
v.to_bits().hash(hasher);
181-
}
182-
Self::U32(v) => {
183-
hasher.write_u8(2);
184-
v.hash(hasher);
185-
}
186-
Self::I32(v) => {
187-
hasher.write_u8(3);
188-
v.hash(hasher);
189-
}
190-
Self::Bool(v) => {
191-
hasher.write_u8(4);
192-
v.hash(hasher);
193-
}
194-
Self::I64(v) => {
195-
hasher.write_u8(5);
196-
v.hash(hasher);
197-
}
198-
Self::U64(v) => {
199-
hasher.write_u8(6);
200-
v.hash(hasher);
201-
}
202-
Self::AbstractInt(v) => {
203-
hasher.write_u8(7);
204-
v.hash(hasher);
205-
}
168+
169+
impl From<crate::Literal> for HashableLiteral {
170+
fn from(l: crate::Literal) -> Self {
171+
match l {
172+
crate::Literal::F64(v) => Self::F64(v.to_bits()),
173+
crate::Literal::F32(v) => Self::F32(v.to_bits()),
174+
crate::Literal::U32(v) => Self::U32(v),
175+
crate::Literal::I32(v) => Self::I32(v),
176+
crate::Literal::U64(v) => Self::U64(v),
177+
crate::Literal::I64(v) => Self::I64(v),
178+
crate::Literal::Bool(v) => Self::Bool(v),
179+
crate::Literal::AbstractInt(v) => Self::AbstractInt(v),
180+
crate::Literal::AbstractFloat(v) => Self::AbstractFloat(v.to_bits()),
206181
}
207182
}
208183
}

0 commit comments

Comments
 (0)