#linked-list #stack-frame #json-path #call-stack #recursion #chained #along

no-std stackstack

A singly linked list intended to be chained along stack frames

4 releases (2 breaking)

0.3.0 Jul 12, 2024
0.2.0 Jul 10, 2024
0.1.1 Jul 10, 2024
0.1.0 Jul 10, 2024

#1076 in Rust patterns

Download history 592/week @ 2024-09-11 521/week @ 2024-09-18 714/week @ 2024-09-25 649/week @ 2024-10-02 298/week @ 2024-10-09 246/week @ 2024-10-16 344/week @ 2024-10-23 293/week @ 2024-10-30 335/week @ 2024-11-06 230/week @ 2024-11-13 85/week @ 2024-11-20 93/week @ 2024-11-27 41/week @ 2024-12-04 53/week @ 2024-12-11 13/week @ 2024-12-18

157 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

19KB
286 lines

A singly linked list intended to be chained along (program) stack frames.

This is useful for visitors and recursive functions.

Example: a JSON visitor


enum Path<'a> {
    Key(&'a str),
    Index(usize),
}

impl std::fmt::Display for Path<'_> { ... }

/// Recursively visit JSON strings, recording their path and contents
fn collect_strs<'a>(
    v: &mut Vec<(String, &'a str)>,
    path: stackstack::Stack<Path>,
    //                ^^^^^^^^^^^ shared across recursive calls
    json: &'a serde_json::Value,
) {
    match json {
        Value::String(it) => v.push((itertools::join(&path, "."), it)),
        //    iterate the path to the current node ~~^
        Value::Array(arr) => {
            for (ix, child) in arr.iter().enumerate() {
                collect_strs(v, path.pushed(Path::Index(ix)), child)
                //              ^~~ recurse with an appended path
            }
        }
        Value::Object(obj) => {
            for (k, child) in obj {
                collect_strs(v, path.pushed(Path::Key(k)), child)
                //                          ^~~ the new node is allocated on
                //                              the current (program) stack frame
            }
        },
        _ => {}
    }
}

let mut v = vec![];
let json = json!({
    "mary": {
        "had": [
            {"a": "little lamb"},
            {"two": "yaks"}
        ]
    }
});
collect_strs(&mut v, Stack::new(), &json);
assert_eq! { v, [
    ("mary.had.0.a".into(), "little lamb"),
    ("mary.had.1.two".into(), "yaks")
]}

No runtime deps