Skip to content

Commit db16038

Browse files
Provide faster Eq/Hash impls for interned data
1 parent 5c38661 commit db16038

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

collector/src/intern.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@ pub trait InternString {
99
#[macro_export]
1010
macro_rules! intern {
1111
(pub struct $for_ty:ident) => {
12-
#[derive(Serialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)]
12+
#[derive(Serialize, Debug, PartialOrd, Ord, Copy, Clone)]
1313
pub struct $for_ty(&'static str);
1414

15+
impl std::cmp::PartialEq for $for_ty {
16+
fn eq(&self, other: &Self) -> bool {
17+
std::ptr::eq(self.0.as_ptr(), other.0.as_ptr())
18+
}
19+
}
20+
21+
impl std::cmp::Eq for $for_ty {}
22+
23+
impl std::hash::Hash for $for_ty {
24+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
25+
state.write_usize(self.0.as_ptr() as usize);
26+
}
27+
}
28+
1529
impl<'de> serde::de::Deserialize<'de> for $for_ty {
1630
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1731
where
@@ -57,6 +71,13 @@ macro_rules! intern {
5771
}
5872
}
5973

74+
impl std::ops::Deref for $for_ty {
75+
type Target = str;
76+
fn deref(&self) -> &str {
77+
self.0
78+
}
79+
}
80+
6081
impl crate::intern::InternString for $for_ty {
6182
fn to_interned(v: &'static str) -> $for_ty {
6283
$for_ty(v)

0 commit comments

Comments
 (0)