Skip to main content

luaur_analysis/records/
sym_def.rs

1use crate::records::symbol::Symbol;
2use alloc::string::String;
3
4#[allow(non_camel_case_types)]
5#[derive(Debug, Clone)]
6pub struct SymDef {
7    pub sym: Symbol,
8    pub version: usize,
9}
10
11impl PartialEq for SymDef {
12    fn eq(&self, other: &Self) -> bool {
13        self.sym.operator_eq_symbol(&other.sym) && self.version == other.version
14    }
15}
16
17impl Eq for SymDef {}
18
19impl core::hash::Hash for SymDef {
20    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
21        self.sym.hash_luau_symbol_operator_call().hash(state);
22        self.version.hash(state);
23    }
24}
25
26#[allow(non_snake_case)]
27impl SymDef {
28    pub fn sym_def(sym: Symbol, version: usize) -> Self {
29        Self { sym, version }
30    }
31
32    pub fn name(&self) -> String {
33        unsafe {
34            let cstr = self.sym.c_str();
35            core::ffi::CStr::from_ptr(cstr)
36                .to_string_lossy()
37                .into_owned()
38        }
39    }
40
41    pub fn versioned_name(&self) -> String {
42        format!("{}-{}", self.name(), self.version)
43    }
44
45    pub fn operator_eq_sym_def(&self, other: &Self) -> bool {
46        self.sym.operator_eq_symbol(&other.sym) && self.version == other.version
47    }
48
49    pub fn operator_ne_sym_def(&self, other: &Self) -> bool {
50        !self.operator_eq_sym_def(other)
51    }
52}
53
54unsafe impl Send for SymDef {}
55unsafe impl Sync for SymDef {}