Skip to content

Commit

Permalink
Implement DeclUniqueId support for ParsedDeclId.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Mar 11, 2024
1 parent b8de3b4 commit 0be07fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sway-core/src/decl_engine/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<T> fmt::Debug for DeclId<T> {
}

#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct DeclUniqueId(u64);
pub struct DeclUniqueId(pub(crate) u64);

impl<T> DeclId<T> {
pub(crate) fn inner(&self) -> DeclIdIndexType {
Expand Down
14 changes: 14 additions & 0 deletions sway-core/src/decl_engine/parsed_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::hash::{DefaultHasher, Hasher};
use std::marker::PhantomData;
use std::{fmt, hash::Hash};

use super::DeclUniqueId;

pub type ParsedDeclIdIndexType = usize;

/// An ID used to refer to an item in the [ParsedDeclEngine](super::decl_engine::ParsedDeclEngine)
Expand All @@ -16,6 +19,17 @@ impl<T> ParsedDeclId<T> {
pub(crate) fn inner(&self) -> ParsedDeclIdIndexType {
self.0
}

pub fn unique_id(&self) -> DeclUniqueId
where
T: 'static,
{
let mut hasher = DefaultHasher::default();
std::any::TypeId::of::<T>().hash(&mut hasher);
self.0.hash(&mut hasher);

DeclUniqueId(hasher.finish())
}
}

impl<T> Copy for ParsedDeclId<T> {}
Expand Down

0 comments on commit 0be07fd

Please sign in to comment.