Crate databake

source ·
Expand description

This crate allows data to write itself into Rust code (bake itself in).

Types that implement the Bake trait can be written into Rust expressions, which allows using Rust code itself as a zero-overhead “serialization” strategy.

§Example

use databake::*;
use alloc::borrow::Cow;

let data = [Some((18, Cow::Borrowed("hi")))];
assert_eq!(
    data.bake(&Default::default()).to_string(),
    r#"[Some ((18i32 , alloc :: borrow :: Cow :: Borrowed ("hi")))]"#,
);

§Derive

Bake can be automatically derived if the derive Cargo feature is enabled.

use databake::*;

#[derive(Bake)]
#[databake(path = my_crate)]
struct MyStruct {
    number: u32,
    string: &'static str,
    slice: &'static [bool],
}

#[derive(Bake)]
#[databake(path = my_crate)]
struct AnotherOne(MyStruct, char);

§Testing

The test_bake macro can be used to assert that a particular expression is a Bake fixed point.

test_bake!(
    AnotherOne,
    const: crate::AnotherOne(
        crate::MyStruct {
          number: 17u32,
          string: "foo",
          slice: &[true, false],
        },
        'b',
    ),
    my_crate,
);

Re-exports§

Macros§

  • This macro tests that an expression evaluates to a value that bakes to the same expression.

Structs§

  • A collection of crates that are required for the evaluation of some expression.

Traits§

  • The Bake trait allows a piece of data to write itself into a Rust expression.

Derive Macros§

  • This custom derive auto-implements the Bake trait on any given type that has public fields that also implement Bake.