Skip to main content

microcad_core/
hash.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Render hash functionality.
5
6use std::hash::Hasher;
7
8use derive_more::Deref;
9
10/// Render hash type.
11pub type HashId = u64;
12
13pub use rustc_hash::FxHashMap as HashMap;
14pub use rustc_hash::FxHashSet as HashSet;
15
16/// Trait to implement for typed that contain a pre-computed hash value.
17pub trait ComputedHash {
18    /// Return computed hash value.
19    fn computed_hash(&self) -> HashId;
20}
21
22/// Generic wrapper that contains the hashed value.
23#[derive(Deref, Debug, Clone)]
24pub struct Hashed<T: std::hash::Hash> {
25    #[deref]
26    inner: T,
27    hash: HashId,
28}
29
30impl<T: std::hash::Hash> Hashed<T> {
31    /// Create a new wrapper with hashed.
32    pub fn new(inner: T) -> Self {
33        let mut hasher = rustc_hash::FxHasher::default();
34        inner.hash(&mut hasher);
35        Self {
36            inner,
37            hash: hasher.finish(),
38        }
39    }
40}
41
42impl<T: std::hash::Hash> ComputedHash for Hashed<T> {
43    fn computed_hash(&self) -> HashId {
44        self.hash
45    }
46}