4 releases

0.1.3 Jul 5, 2022
0.1.2 Jul 4, 2022
0.1.1 Jul 4, 2022
0.1.0 Jul 4, 2022

#2132 in Rust patterns

Apache-2.0

18KB
253 lines

🥯 bagel: Always baked, never fried

Rust stable docs.rs Crates.io Discord GitHub Workflow Status

bagel 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.

Importing

bagel = "0.1"

What bagel can do

  • def: Use the default declaration syntax
  • Ctor: Derive constructors:
    • Full lifetimes, generics and where clause support
    • #[phantom]: Auto elide PhantomData fields
    • #[ctor_const]: Make the constructor a const fn
  • Gtor: Derive getters:
    • Full lifetimes, generics and where clause support
    • Advanced attributes: #[gtor_const], #[gtor_copy], #[gtor_skip], #[phantom] and #[gtor]
  • Stor: Derive setters
    • Full lifetimes, generics and where clause support
    • Skip setter with #[stor_skip] or #[phantom]
  • Constdef: Derive constant, compile-time default implementations. See an example here

Default declaration syntax

The default declaration syntax is an alternative way to implement defaults for your structs (and enums soon). It looks like this:

  1. Use the default trait:
    field: type
    
  2. Use your specified expression:
    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 example

use 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);

License

The dough and bagel libraries are distributed under the Apache-2.0 License.

Dependencies

~1.5MB
~34K SLoC