makepad_live_compiler/
live_ptr.rs1#![allow(dead_code)]
2
3use{
4 crate::{
5 makepad_live_tokenizer::LiveId,
6 },
7 std::fmt,
8};
9
10#[derive(Clone, Copy, Default, Debug, Eq, Ord, PartialOrd, Hash, PartialEq)]
11pub struct LiveFileId(pub u16);
12
13impl LiveFileId {
14 pub fn new(index: usize) -> LiveFileId {LiveFileId(index as u16)}
15 pub fn to_index(&self) -> usize {self.0 as usize}
16}
17
18#[derive(Default, Clone, Eq, Hash, Debug, Copy, PartialEq, PartialOrd, Ord)]
20pub struct LiveModuleId(pub LiveId, pub LiveId);
21
22impl LiveModuleId {
23 pub fn from_str(module_path: &str) -> Result<Self,
24 String> {
25 let bytes = module_path.as_bytes();
27 let len = bytes.len();
28 let mut crate_id = LiveId(0);
30 let mut i = 0;
31 while i < len {
32 if bytes[i] == b':' {
33 crate_id = LiveId::from_str_with_lut(std::str::from_utf8(&bytes[0..i]).unwrap()) ?;
34 i += 2;
35 break
36 }
37 i += 1;
38 }
39 if i == len { Ok(LiveModuleId(LiveId(0), LiveId::from_str_with_lut(std::str::from_utf8(&bytes[0..len]).unwrap()) ?))
41 } else {
42 Ok(LiveModuleId(crate_id, LiveId::from_str_with_lut(std::str::from_utf8(&bytes[i..len]).unwrap()) ?))
43 }
44 }
45
46}
47
48impl fmt::Display for LiveModuleId {
49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 write!(f, "{}::{}", self.0, self.1)
51 }
52}
53#[derive(Copy, Default, Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
59pub struct LiveFileGeneration(u16);
60
61impl LiveFileGeneration{
62 pub fn next_gen(&mut self){
63 self.0+=1
64 }
65}
66
67impl fmt::Display for LiveFileGeneration {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 write!(f, "{}", self.0)
70 }
71}
72
73#[derive(Clone, Debug, Eq, Hash, Copy, Ord, PartialOrd, PartialEq)]
74pub struct LivePtr {
75 pub file_id: LiveFileId,
76 pub generation: LiveFileGeneration,
77 pub index: u32,
78}
79
80pub type LiveRef = Option<LivePtr>;
81
82impl LivePtr{
83 pub fn invalid()->Self{
84 Self{
85 file_id: LiveFileId::default(),
86 generation: LiveFileGeneration(u16::MAX),
87 index: 0
88 }
89 }
90
91 pub fn is_invalid(&self)->bool{
92 self.generation.0 == u16::MAX
93 }
94
95 pub fn node_index(&self)->usize{
96 self.index as usize
97 }
98
99 pub fn with_index(&self, index:usize)->Self{
100 Self{file_id:self.file_id, index:index as u32, generation:self.generation}
101 }
102
103 pub fn from_index(file_id:LiveFileId, index:usize, generation:LiveFileGeneration)->Self{
104 Self{file_id, index:index as u32, generation}
105 }
106}
107
108impl fmt::Display for LivePtr {
109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 write!(f, "{}_{}", self.file_id.0, self.index)
111 }
112}