Skip to main content

Crate lazy_bastard

Crate lazy_bastard 

Source
Expand description

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
         },
      }
   }
}

Macros§

code_in_scope
an internal macro for other functions, returns input code without creating an inner scope
lazy_bastard
inline defaults for struct definitions if your lazy
time_section_print
times a section of code printing the elapsed time, takes an optional name before the code