Skip to content

Commit 19ad90e

Browse files
committed
Use new dyn eq/hash traits for MagicAny
1 parent f993d51 commit 19ad90e

File tree

1 file changed

+8
-46
lines changed

1 file changed

+8
-46
lines changed

turbopack/crates/turbo-tasks/src/magic_any.rs

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@ use core::fmt;
22
use std::{
33
any::{Any, TypeId},
44
fmt::Debug,
5-
hash::{Hash, Hasher},
6-
ops::DerefMut,
5+
hash::Hash,
76
sync::Arc,
87
};
98

109
use serde::{Deserialize, Serialize, de::DeserializeSeed};
10+
use turbo_dyn_eq_hash::{
11+
DynEq, DynHash, DynPartialEq, impl_eq_for_dyn, impl_hash_for_dyn, impl_partial_eq_for_dyn,
12+
};
1113

1214
use crate::trace::{TraceRawVcs, TraceRawVcsContext};
1315

14-
pub trait MagicAny: mopa::Any + Send + Sync {
16+
pub trait MagicAny: mopa::Any + DynPartialEq + DynEq + DynHash + Send + Sync {
1517
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send>;
1618

1719
fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result;
1820

19-
fn magic_eq(&self, other: &dyn MagicAny) -> bool;
20-
21-
fn magic_hash(&self, hasher: &mut dyn Hasher);
22-
2321
fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext);
2422

2523
#[cfg(debug_assertions)]
@@ -51,17 +49,6 @@ impl<T: Debug + Eq + Hash + Send + Sync + TraceRawVcs + 'static> MagicAny for T
5149
d.finish()
5250
}
5351

54-
fn magic_eq(&self, other: &dyn MagicAny) -> bool {
55-
match other.downcast_ref::<Self>() {
56-
None => false,
57-
Some(other) => self == other,
58-
}
59-
}
60-
61-
fn magic_hash(&self, hasher: &mut dyn Hasher) {
62-
Hash::hash(&(TypeId::of::<Self>(), self), &mut HasherMut(hasher))
63-
}
64-
6552
fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
6653
self.trace_raw_vcs(trace_context);
6754
}
@@ -78,34 +65,9 @@ impl fmt::Debug for dyn MagicAny {
7865
}
7966
}
8067

81-
impl PartialEq for dyn MagicAny {
82-
fn eq(&self, other: &Self) -> bool {
83-
self.magic_eq(other)
84-
}
85-
}
86-
87-
impl Eq for dyn MagicAny {}
88-
89-
impl Hash for dyn MagicAny {
90-
fn hash<H: Hasher>(&self, hasher: &mut H) {
91-
self.magic_hash(hasher)
92-
}
93-
}
94-
95-
pub struct HasherMut<H: ?Sized>(pub H);
96-
97-
impl<H: DerefMut + ?Sized> Hasher for HasherMut<H>
98-
where
99-
H::Target: Hasher,
100-
{
101-
fn finish(&self) -> u64 {
102-
self.0.finish()
103-
}
104-
105-
fn write(&mut self, bytes: &[u8]) {
106-
self.0.write(bytes)
107-
}
108-
}
68+
impl_partial_eq_for_dyn!(dyn MagicAny);
69+
impl_eq_for_dyn!(dyn MagicAny);
70+
impl_hash_for_dyn!(dyn MagicAny);
10971

11072
impl TraceRawVcs for dyn MagicAny {
11173
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {

0 commit comments

Comments
 (0)