| 73 | |
| 74 | impl TryClone for StringPool { |
| 75 | fn try_clone(&self) -> Result<Self, OutOfMemory> { |
| 76 | let mut new_pool = StringPool::new(); |
| 77 | // Re-intern strings in index order so that each Atom value is |
| 78 | // identical in the clone — callers that hold Atoms from the original |
| 79 | // can use them interchangeably with the clone. |
| 80 | // |
| 81 | // Directly cloning `self.map` would copy &'static str keys that point |
| 82 | // into the *original* pool's `strings` allocation. Those pointers |
| 83 | // become dangling once the original is dropped, leading to UB on any |
| 84 | // subsequent lookup. Re-interning ensures the cloned map's keys point |
| 85 | // into the clone's own `strings`. |
| 86 | for s in self.strings.iter() { |
| 87 | new_pool.insert(s)?; |
| 88 | } |
| 89 | Ok(new_pool) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl TryClone for Atom { |