luaur_bytecode/records/
string_ref.rs1use core::hash::{Hash, Hasher};
2
3#[derive(Debug, Clone, Copy, Default)]
4pub struct StringRef {
5 pub(crate) data: *const i8,
6 pub(crate) length: usize,
7}
8
9impl StringRef {
10 pub fn new(data: *const i8, length: usize) -> Self {
11 Self { data, length }
12 }
13
14 #[inline]
15 fn as_bytes(&self) -> &[u8] {
16 if self.data.is_null() {
17 &[]
18 } else {
19 unsafe { core::slice::from_raw_parts(self.data as *const u8, self.length) }
20 }
21 }
22}
23
24impl PartialEq for StringRef {
32 fn eq(&self, other: &Self) -> bool {
33 if !self.data.is_null() && !other.data.is_null() {
34 self.length == other.length && self.as_bytes() == other.as_bytes()
35 } else {
36 self.data == other.data
37 }
38 }
39}
40
41impl Eq for StringRef {}
42
43impl Hash for StringRef {
46 fn hash<H: Hasher>(&self, state: &mut H) {
47 self.as_bytes().hash(state);
48 }
49}