Get the latest tech news
What Is in a Rust Allocator?
Weak Opinions, Strongly Held
UnsafeCell gives us the required escape hatch to return a mutable pointer into a struct we only have a shared reference to. We recall that remaining is an AtomicUsize describing an offset from the start of the arena, counting down as we allocate memory. As mentioned above, if we were not using an atomic, either the compiler or the CPU itself could decide to reorder the different reads and writes to the counter for performance optimizations, compromising correctness.
Or read this on Hacker News