[][src]Module fasteval::slab

A Slab is a pre-allocated block of memory, used during the parse/compile/eval phases to reduce memory allocation/deallocation.

You usually won't need to use any of the methods of a Slab; you'll just pass it to other functions (sort of like a Context in other systems).

The Slab contains two fields: ps ("Parse Slab") and cs ("Compile Slab"). It is structured like this because of Rust's borrowing rules, so that the two fields can be borrowed and mutated independently.

If you use the ez_eval() function, it allocates a Slab for you.

If you are performing the parse/compile/eval process yourself, then you'll need to allocate a Slab at the beginning.

Examples

Here is an example of re-using one Slab for multiple parse/eval cycles:

use fasteval::Evaler;  // import this trait so we can call eval().
fn main() -> Result<(), fasteval::Error> {
    let mut slab = fasteval::Slab::new();

    let val = fasteval::parse("1+2*3-4", &mut slab.ps)?.from(&slab.ps).eval(&slab, &mut fasteval::EmptyNamespace)?;
    assert_eq!(val, 3.0);

    // Let's re-use the same slab again to save memory operations.
    // Clear out the previous data:
    slab.clear();

    let val = fasteval::parse("5+6*7-8", &mut slab.ps)?.from(&slab.ps).eval(&slab, &mut fasteval::EmptyNamespace)?;
    assert_eq!(val, 39.0);

    Ok(())
}

Structs

CompileSlab
ParseSlab
Slab