#lazy-evaluation #default #effort #macro #seperate #bastard #writeing

lazy_bastard

A helpfull macro because writeing a seperate Default function is too much effort

7 releases

0.1.6 Oct 13, 2024
0.1.5 Oct 13, 2024

#1302 in Rust patterns

MIT license

8KB
85 lines

inline defaults for struct definitions if you’re lazy, plus some other stuff, probably.

makes creating simple structs that don’t need a constructor like settings structs much clearer

somewhat second guessing the name, but it’s too late to change it

    use lazy_bastard::lazy_bastard;

    lazy_bastard!(
       #[derive(Clone, Debug)]
       pub struct MyStruct<'a> { // lifetime is just for the example
          normal: i32 => 100_324,
          function_call: String => "test".into(),
          automatic: f64, // uses Default::default() instead
          scoped: f32 => {
             let c: f32 = 1.2;
             let v = c.abs().sin().sin().sqrt();
             0.1 * v
          }
       }
    );


    impl MyStruct {
        fn new() -> Self { Self::default() }
    }


    // Compiles into //

    #[derive(Clone, Debug)]
    pub struct CompiledStruct<'a> {
       normal: i32,
       function_call: String,
       automatic: f64,
       scoped: f32,
    }
    impl<'a> Default for CompiledStruct<'a> {
       fn default() -> Self {
          Self {
             normal: 100_324,
             function_call: ("test".into()),
             automatic: Default::default(),
             scoped: {
                let c: f32 = 1.2;
                let v = c.abs().sin().sin().sqrt();
                0.1 * v
             },
          }
       }
    }

No runtime deps