#proc-macro #macro-derive #if-empty #is-empty

macro if_empty_derive

Proc macro to generate if_empty for types that have is_empty implemented

1 unstable release

0.1.0 Jul 30, 2021

#945 in #macro-derive


Used in if_empty

MPL-2.0 license

7KB

IfEmpty derive macro

This crate provides a derive macro implementing the if_empty functionality if the type has an is_empty function.

#[derive(IfEmpty)]
struct Example {
    value: String,
}

impl Example {
    fn is_empty(&self) -> bool {
        self.value.is_empty()
    }
}

fn main() {
    let example = Example {
        value: String::new(),
    };
    print!("Example '{}'", example.value);
    // Example ''

    let not_empty_example = e.if_empty(Example {
        value: "default val".to_string(),
    });

    print!("Example '{}'", not_empty_example.value);
    // Example 'default val'
}

lib.rs:

Derive macro complementing if_empty

This crate provides a derive macro implementing the if_empty function if the type has a is_empty function.

Examples

#[derive(IfEmpty)]
struct Example {
    value: String,
}

impl Example {
    fn is_empty(&self) -> bool {
        self.value.is_empty()
    }
}

let example = Example {
    value: String::new(),
};

assert!(example.value.is_empty());
assert_eq!(example.if_empty(Example {value: "a default".to_string()}).value, "a default");

Dependencies

~1.5MB
~37K SLoC