deki_macros

Crates.iodeki_macros
lib.rsdeki_macros
version0.1.2
created_at2024-12-27 06:01:19.281527+00
updated_at2025-10-26 03:15:37.87403+00
descriptionA growing set of macros (tailored to myself)!
homepage
repositoryhttps://github.com/dekirisu/deki-rs/
max_upload_size
id1496221
size15,887
Dekirisu (dekirisu)

documentation

README

My Macros

Various macros I made and liked to use occasionally!

Attach functionality to type variants

Basically write this:

 enum Object {RedSphere, GreenCube}
 match_fns!{

     // 1. Define Methods - '&self' is assumed as parameter
     [Object]
     shape() -> &'static str;
     color(brightness:f32) -> &'static str;

     // 2. Add Code
     [::RedSphere]
     shape: "sphere";
     color: if brightness > 0.5 {"bright-red"} else {"red"};

     [::GreenCube]
     shape: "cube";
     color: "just-green";

 }

Instead of:

impl Object {
    pub fn shape(&self) -> &'static str {
        match self {
            Color::RedSphere => "sphere",
            Color::GreenCube => "cube",
        }
    }
    pub fn color(&self, brightness: f32) -> &'static str {
        match self {
            Color::RedSphere => {
                if brightness > 0.5 {"bright-red"} else {"red"}
            }
            Color::GreenCube => "just-green",
        }
    }
}

Quick Implementations

Alternative syntax for implementing trait with ONE required method.

quimp!{StructName
   fn clone(&self) -> Self {Self::new(self.0)};
   fn default() -> Self {Self::new(100)};
}

Or using this:

  • #[imp(Struct)]: for a owned Type
  • #[imp(Struct|Trait)]: to impl a singe-method trait
  • #[imp(Struct|*)]: for a foreign Type (generates a new trait)
#[imp(Struct)]
fn function(){}

Bool Aliases

Simple macro that replaces X -> true and O -> false:

xoxo!{match [true,false,true] {
    [O,O,O] => "nope",
    [O,O,X] => "nope",
    [X,O,X] => "YEP!",
    [_,_,_] => "nope"
}}
Commit count: 34

cargo fmt