Skip to main content

luaur_analysis/records/
symbol.rs

1use luaur_ast::records::ast_local::AstLocal;
2use luaur_ast::records::ast_name::AstName;
3
4#[derive(Debug, Clone)]
5pub struct Symbol {
6    pub(crate) local: *mut AstLocal,
7    pub(crate) global: AstName,
8}
9
10impl Default for Symbol {
11    fn default() -> Self {
12        Self {
13            local: core::ptr::null_mut(),
14            global: AstName::new(),
15        }
16    }
17}
18
19impl Symbol {
20    pub fn from_local(local: *mut AstLocal) -> Self {
21        Self {
22            local,
23            global: AstName::new(),
24        }
25    }
26
27    pub fn from_global(global: AstName) -> Self {
28        Self {
29            local: core::ptr::null_mut(),
30            global,
31        }
32    }
33}
34
35impl PartialEq for Symbol {
36    fn eq(&self, rhs: &Self) -> bool {
37        if !self.local.is_null() {
38            self.local == rhs.local
39        } else if !self.global.value.is_null() {
40            !rhs.global.value.is_null()
41                && unsafe {
42                    core::ffi::CStr::from_ptr(self.global.value)
43                        == core::ffi::CStr::from_ptr(rhs.global.value)
44                }
45        } else {
46            rhs.local.is_null() && rhs.global.value.is_null()
47        }
48    }
49}
50
51impl Eq for Symbol {}
52
53impl core::hash::Hash for Symbol {
54    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
55        self.local.hash(state);
56
57        if !self.global.value.is_null() {
58            unsafe {
59                core::ffi::CStr::from_ptr(self.global.value)
60                    .to_bytes()
61                    .hash(state)
62            };
63        }
64    }
65}
66
67unsafe impl Send for Symbol {}
68unsafe impl Sync for Symbol {}