Crates.io | databake |
lib.rs | databake |
version | 0.1.8 |
source | src |
created_at | 2022-06-28 15:14:23.95864 |
updated_at | 2024-05-28 19:57:44.159619 |
description | Trait that lets structs represent themselves as (const) Rust expressions |
homepage | |
repository | https://github.com/unicode-org/icu4x |
max_upload_size | |
id | 614897 |
size | 23,400 |
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.
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")))]"#,
);
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);
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,
);
For more information on development, authorship, contributing etc. please visit ICU4X home page
.