Crates.io | rustructure-macros |
lib.rs | rustructure-macros |
version | 0.2.0 |
source | src |
created_at | 2022-05-20 23:05:25.459239 |
updated_at | 2022-05-20 23:05:25.459239 |
description | Macros for rustructure crate. |
homepage | |
repository | https://git.flying-kestrel.ca/kestrel/rustructure |
max_upload_size | |
id | 590511 |
size | 5,303 |
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.