vortex_expr/
scope_vars.rs1use std::any::{Any, TypeId};
5use std::hash::{BuildHasherDefault, Hasher};
6
7use vortex_utils::aliases::hash_map::HashMap;
8
9pub(crate) type ScopeVars = HashMap<TypeId, Box<dyn ScopeVar>, BuildHasherDefault<IdHasher>>;
11
12#[derive(Default)]
16pub(super) struct IdHasher(u64);
17
18impl Hasher for IdHasher {
19 #[inline]
20 fn finish(&self) -> u64 {
21 self.0
22 }
23
24 fn write(&mut self, _: &[u8]) {
25 unreachable!("TypeId calls write_u64");
26 }
27
28 #[inline]
29 fn write_u64(&mut self, id: u64) {
30 self.0 = id;
31 }
32}
33
34pub trait ScopeVar: Any + Send + Sync {
36 fn as_any(&self) -> &dyn Any;
37 fn as_any_mut(&mut self) -> &mut dyn Any;
38 fn clone_box(&self) -> Box<dyn ScopeVar>;
39}
40
41impl Clone for Box<dyn ScopeVar> {
42 fn clone(&self) -> Self {
43 (**self).clone_box()
44 }
45}
46
47impl<T: Clone + Send + Sync + 'static> ScopeVar for T {
48 fn as_any(&self) -> &dyn Any {
49 self
50 }
51
52 fn as_any_mut(&mut self) -> &mut dyn Any {
53 self
54 }
55
56 fn clone_box(&self) -> Box<dyn ScopeVar> {
57 Box::new(self.clone())
58 }
59}