cairo_lang_semantic/
ids.rs

1use std::hash::{Hash, Hasher};
2use std::sync::Arc;
3
4use cairo_lang_proc_macros::HeapSize;
5use cairo_lang_utils::define_short_id;
6use salsa::Database;
7
8use crate::plugin::AnalyzerPlugin;
9
10/// An Id allowing interning [`AnalyzerPlugin`] into Salsa database.
11#[derive(Clone, Debug, HeapSize)]
12pub struct AnalyzerPluginLongId(pub Arc<dyn AnalyzerPlugin>);
13
14impl AnalyzerPlugin for AnalyzerPluginLongId {
15    fn diagnostics<'db>(
16        &self,
17        db: &'db dyn Database,
18        module_id: cairo_lang_defs::ids::ModuleId<'db>,
19    ) -> Vec<cairo_lang_defs::plugin::PluginDiagnostic<'db>> {
20        self.0.diagnostics(db, module_id)
21    }
22
23    fn declared_allows(&self) -> Vec<String> {
24        self.0.declared_allows()
25    }
26
27    fn plugin_type_id(&self) -> std::any::TypeId {
28        // Ensure the implementation for `AnalyzerPluginLongId` returns the same value
29        // as the underlying plugin object.
30        self.0.plugin_type_id()
31    }
32}
33
34// `PartialEq` and `Hash` cannot be derived on `Arc<dyn ...>`,
35// but pointer-based equality and hash semantics are enough in this case.
36impl PartialEq for AnalyzerPluginLongId {
37    fn eq(&self, other: &Self) -> bool {
38        Arc::ptr_eq(&self.0, &other.0)
39    }
40}
41
42impl Eq for AnalyzerPluginLongId {}
43
44impl Hash for AnalyzerPluginLongId {
45    fn hash<H: Hasher>(&self, state: &mut H) {
46        Arc::as_ptr(&self.0).hash(state)
47    }
48}
49
50define_short_id!(AnalyzerPluginId, AnalyzerPluginLongId);