temp-stack 1.0.1

A data structure for contexts or similar stack structures that are allocated on the call stack, using the temp-inst crate for lifetime erasure.
Documentation
  • Coverage
  • 57.14%
    12 out of 21 items documented1 out of 12 items with examples
  • Size
  • Source code size: 24.1 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.22 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • SReichelt/temp-inst
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • SReichelt

temp-stack crate

TempStack is a linked list data structure based on the temp-inst crate. The intended use case is that list items are allocated on the call stack; then the list also represents a "stack" with "frames". Via temp-inst, each frame can contain references to data that is available at the point where it is constructed, without having to add lifetime parameters.

Example

A parser or compiler or interpreter can use a TempStack reference as a context that is passed to individual functions, to determine which variables are in scope. For example, a context with just variable names might be defined as

type Ctx = TempStack<(), TempRef<str>>;

Due to the use of TempRef from the temp-inst crate, no lifetime parameters are required to access local string slices.

A function can construct construct a new context with an added variable, and pass it to another function:

fn parse_expr<'a>(s: &'a str, ctx: &Ctx) -> (Expr, &'a str) {
    // ...
    if is_lambda {
        let (s, name) = parse_name(s);
        let body_ctx = ctx.new_frame(name);
        return parse_expr(s, &body_ctx);
    }
    // ...
}

We can easily iterate over a context to find a given variable:

fn parse_expr<'a>(s: &'a str, ctx: &Ctx) -> (Expr, &'a str) {
    // ...
    if is_var {
        let (s, name) = parse_name(s);
        // Determine the De Bruijn index of the nearest `name` in context.
        let Some(idx) = ctx.iter().position(|v| v == name) else ...
        // ...
    }
}

See the documentation for the complete example.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.