Interns a string, returning its dynamic atom bits.
(&self, s: &str)
| 101 | |
| 102 | /// Interns a string, returning its dynamic atom bits. |
| 103 | fn atomize(&self, s: &str) -> u32 { |
| 104 | if let Some(&bits) = self.str_to_bits_map.read().unwrap().get(s) { |
| 105 | return bits; |
| 106 | } |
| 107 | |
| 108 | let mut str_to_bits = self.str_to_bits_map.write().unwrap(); |
| 109 | let mut bits_to_str = self.bits_to_str_vec.write().unwrap(); |
| 110 | |
| 111 | if let Some(&bits) = str_to_bits.get(s) { |
| 112 | return bits; |
| 113 | } |
| 114 | |
| 115 | let id = bits_to_str.len() as u32; |
| 116 | let bits = id | (1 << ATOM_DYNAMIC_BIT); // Set dynamic bit |
| 117 | |
| 118 | // Leak the string to get a 'static reference - this is an intern pool |
| 119 | // where strings live for the entire program lifetime anyway due to static |
| 120 | // lifetime of singleton instances. |
| 121 | let static_str: &'static str = Box::leak(s.into()); |
| 122 | bits_to_str.push(static_str); |
| 123 | str_to_bits.insert(static_str, bits); |
| 124 | |
| 125 | bits |
| 126 | } |
| 127 | |
| 128 | /// Looks up a string by its dynamic atom bits. |
| 129 | fn lookup(&self, bits: u32) -> Option<&'static str> { |