pub fn scratch_arena(conflict: Option<&Arena>) -> ScratchArena<'static>Expand description
Need an arena for temporary allocations? scratch_arena got you covered.
Call scratch_arena and it’ll return an Arena that resets when it goes out of scope.
Most methods make just two kinds of allocations:
- Interior: Temporary data that can be deallocated when the function returns.
- Exterior: Data that is returned to the caller and must remain alive until the caller stops using it.
Such methods only have two lifetimes, for which you consequently also only need two arenas. …even if your method calls other methods recursively! This is because the exterior allocations of a callee are simply interior allocations to the caller, and so on, recursively.
This works as long as the two arenas flip/flop between being used as interior/exterior allocator along the callstack. To ensure that is the case, we use a recursion counter in debug builds.
This approach was described among others at: https://nullprogram.com/blog/2023/09/27/
§Safety
If your function takes an Arena argument, you MUST pass it to scratch_arena as Some(&arena).