pub struct ArenaBox<T: WithLifetime> { /* private fields */ }Expand description
A smart pointer that holds a struct with arena allocated objects and the arena in the same struct.
This is useful for creating self-referential structs.
§Example
pub struct Data<'a> {
msg: &'a str,
}
make_arena_version!(Data, pub ArenaData);
let boxed = ArenaData::new(|arena| Data {
msg: arena.alloc_str("Something"),
});
assert_eq!(boxed.get().msg, "Something");Implementations§
Source§impl<T: WithLifetime> ArenaBox<T>
impl<T: WithLifetime> ArenaBox<T>
Sourcepub fn new<F>(build: F) -> Self
pub fn new<F>(build: F) -> Self
Creates a new ArenaBox.
§Example
pub struct Data<'a> {
msg: &'a str,
}
make_arena_version!(Data, pub ArenaData);
let boxed = ArenaData::new(|arena| Data {
msg: arena.alloc_str("Something"),
});
assert_eq!(boxed.get().msg, "Something");Sourcepub fn new_from<U: WithLifetime, F>(source: ArenaBox<U>, build: F) -> Selfwhere
F: for<'a> FnOnce(&'a Bump, &'a <U as WithLifetime>::With<'a>) -> <T as WithLifetime>::With<'a>,
pub fn new_from<U: WithLifetime, F>(source: ArenaBox<U>, build: F) -> Selfwhere
F: for<'a> FnOnce(&'a Bump, &'a <U as WithLifetime>::With<'a>) -> <T as WithLifetime>::With<'a>,
Creates a new ArenaBox by transforming data from another ArenaBox, reusing its arena.
This allows you to build up data structures incrementally, where new types can reference
data from the original type. The source ArenaBox is consumed and its arena is moved
into the new ArenaBox.
§Example
#[derive(Debug, PartialEq)]
pub struct Data<'a> {
msg: &'a str,
}
#[derive(Debug, PartialEq)]
pub struct AugmentedData<'a> {
original: &'a Data<'a>,
extra: &'a str,
}
make_arena_version!(Data, pub ArenaData);
make_arena_version!(AugmentedData, pub ArenaAugmentedData);
let data = ArenaData::new(|arena| Data {
msg: arena.alloc_str("hello"),
});
// Transform Data into AugmentedData, reusing the arena
let augmented = ArenaAugmentedData::new_from(data, |arena, original| {
AugmentedData {
original, // Reference to the original Data!
extra: arena.alloc_str("extra info"),
}
});
assert_eq!(augmented.get().original.msg, "hello");
assert_eq!(augmented.get().extra, "extra info");Sourcepub fn get<'b>(&'b self) -> &'b <T as WithLifetime>::With<'b>
pub fn get<'b>(&'b self) -> &'b <T as WithLifetime>::With<'b>
Get a reference to the data within the arena.
§Safety
The returned reference is valid for the lifetime of the ArenaBox.
§Negative compilation test
The following code should fail to compile because the lifetime of the
reference to the Data struct is tied to the ArenaData instance, and
cannot be extended beyond the lifetime of the ArenaData instance.
let message: &str;
{
let boxed = ArenaData::new(|arena| Data {
msg: arena.alloc_str("Something"),
});
message = boxed.get().msg; // Should fail: cannot extend lifetime
}
assert_eq!(message, "Something");Sourcepub fn mutate<'b>(&'b mut self) -> MutHandle<'b, T>
pub fn mutate<'b>(&'b mut self) -> MutHandle<'b, T>
Mutates the data in the ArenaBox.
This method returns a MutHandle that can be used to mutate the data.
§Example
pub struct Data<'a> {
msg: &'a str,
}
make_arena_version!(Data, pub ArenaData);
let mut boxed = ArenaData::new(|arena| Data {
msg: arena.alloc_str("Something"),
});
assert_eq!(boxed.get().msg, "Something");
{
let mut handle = boxed.mutate();
handle.msg = handle.arena().alloc_str("Something different");
}
assert_eq!(boxed.get().msg, "Something different");