1 unstable release

0.2.0 May 20, 2022

#5 in #walking


Used in rustructure

BSD-4-Clause

6KB
99 lines

rustructure supports walking over designated Rust types at run-time.

This allows for some level of introspection from the perspective of libraries that consume external types that can be prepared for their consumption. One motivating example is when generating code for serialization or deserialization as a complement to serde when a format requires foreknowledge of the type at a time it is impossible to have an actual instance of the type.

This crate can be no_std by removing the default std feature, though without at least alloc its usefulness is likely limited until generic associated types land in stable.

The main features of this library are the Walkable trait/derive macro and Walker trait. A simple example Walker that produces an S-expression-like representation of a data type is included as StringWalker (unavailable in no_std):

use rustructure::Walkable;

#[derive(Walkable)]
struct ExampleStruct<'l> {
    a: Option<u8>,
    b: &'l str,
    c: (u64, String)
}

assert_eq!(
    rustructure::StringWalker::walk::<ExampleStruct>(),
    "(struct:ExampleStruct \
        (field:a (option integer)) \
        (field:b str) \
        (field:c (tuple \
            (field:0 integer) \
            (field:1 str)\
            )\
        )\
    )".to_string()
);

If it is of interest, you can find test cases for the library (via StringWalker) in the unpublished rustructure-test crate in the source repository.

Dependencies

~1.5MB
~37K SLoC