3 releases
| 0.1.2 | Oct 26, 2025 |
|---|---|
| 0.1.1 | Oct 26, 2025 |
| 0.1.0 | Dec 27, 2024 |
#31 in #myself
Used in 8 crates
(via deki)
34KB
550 lines
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"
}}
Dependencies
~0.7–1.2MB
~21K SLoC