Skip to main content

ArenaBox

Struct ArenaBox 

Source
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>

Source

pub fn new<F>(build: F) -> Self
where F: for<'a> FnOnce(&'a Bump) -> <T as WithLifetime>::With<'a>,

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");
Source

pub fn new_from<U: WithLifetime, F>(source: ArenaBox<U>, build: F) -> Self
where 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");
Source

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");
Source

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");

Trait Implementations§

Source§

impl<T: Debug + WithLifetime> Debug for ArenaBox<T>
where for<'a> T::With<'a>: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Display + WithLifetime> Display for ArenaBox<T>
where for<'a> T::With<'a>: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: WithLifetime> PartialEq for ArenaBox<T>
where for<'a> T::With<'a>: PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ArenaBox<T>

§

impl<T> !Send for ArenaBox<T>

§

impl<T> !Sync for ArenaBox<T>

§

impl<T> !UnwindSafe for ArenaBox<T>

§

impl<T> Freeze for ArenaBox<T>
where NonNull<T>: Freeze,

§

impl<T> Unpin for ArenaBox<T>
where NonNull<T>: Unpin,

§

impl<T> UnsafeUnpin for ArenaBox<T>
where NonNull<T>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.