Crates.io | bagel |
lib.rs | bagel |
version | 0.1.3 |
source | src |
created_at | 2022-07-04 06:37:53.861929 |
updated_at | 2022-07-05 15:04:30.947366 |
description | Compile-time evaluation and other tools |
homepage | |
repository | https://github.com/skytable/bagel |
max_upload_size | |
id | 618793 |
size | 15,198 |
bagel
: Always baked, never friedbagel
is a collection of macros and other things that we frequently use at Skytable,
primarily to get work done at compile-time (because we like it baked :P). This crate contains some of the stuff we use, and we'll add more of the "magic" soon.
bagel = "0.1"
def
: Use the default declaration syntaxCtor
: Derive constructors:
#[phantom]
: Auto elide PhantomData
fields#[ctor_const]
: Make the constructor a const fn
Gtor
: Derive getters:
#[gtor_const]
, #[gtor_copy]
, #[gtor_skip]
, #[phantom]
and #[gtor]
Stor
: Derive setters
#[stor_skip]
or #[phantom]
Constdef
: Derive constant, compile-time default implementations. See an example hereThe default declaration syntax is an alternative way to implement defaults for your structs (and enums soon). It looks like this:
field: type
field: type = expression
Here's an example:
use bagel::def;
def! {
#[derive(Debug)]
pub struct MyOven {
starting_temperature: u8,
increment_temp_by: u8 = 1,
oven_name: &'static str = "my_kitchen_wifi_oven1",
items_to_bake: [&'static str; 4] = [
"bagels",
"hashbrowns",
"cookies",
"pie",
],
people_buffer: Vec<String> = vec![
"one for Jamie".into(),
"another for Sophie".into()
],
}
}
let mut myoven = MyOven::default();
assert_eq!(myoven.starting_temperature, 0);
assert_eq!(myoven.oven_name, "my_kitchen_wifi_oven1");
assert_eq!(myoven.items_to_bake[3], "pie");
assert_eq!(myoven.people_buffer.len(), 2);
Constdef
exampleuse bagel::Constdef;
#[derive(Constdef)]
struct Port {
requests: usize,
admin: bool,
}
#[derive(Constdef)]
struct PortLogger {
ports: [Port; 65536],
root_pid: usize,
}
const PORT_LOGGER: PortLogger = PortLogger::default();
assert_eq!(PORT_LOGGER.ports[0].requests, 0);
assert_eq!(PORT_LOGGER.ports[65535].admin, false);
The dough
and bagel
libraries are distributed under the Apache-2.0 License.