bitwise_boolean

Crates.iobitwise_boolean
lib.rsbitwise_boolean
version0.1.0
sourcesrc
created_at2024-06-20 23:08:54.961812
updated_at2024-06-20 23:08:54.961812
descriptionA Rust macro to generate bitwise boolean getters and setters for a struct with u8 field storing up to 8 flags.
homepage
repositoryhttps://github.com/SolWorks-Dev/bitwise_boolean
max_upload_size
id1278876
size6,572
Zhe (zhe-t)

documentation

README

bitwise_boolean

A Rust macro to generate bitwise boolean getters and setters for a struct.

Usage

To use the bitwise_boolean macro, add the following to your Cargo.toml file:

[dependencies]
bitwise_boolean = "0.1.0"

Then, use the macro in your code:

use bitwise_boolean::BitwiseBoolean;

#[derive(Default, BitwiseBoolean)]
struct ExampleSettingsData {
    // up to 8 flags total
    #[bitwise_bool(
        admin,
        agreed_to_terms,
        banned,
        random_other_flag
    )]
    pub booleans: u8,
}

struct ExampleSettings {
    data: ExampleSettingsData,
}

impl ExampleSettings {
    fn new() -> Self {
        Self {
            data: ExampleSettingsData::default(),
        }
    }
}

Now, you can use the bitwise boolean getters and setters in your code:

let mut settings = ExampleSettings::new();

settings.data.set_admin(true);
assert!(settings.data.admin());
assert!(!settings.data.agreed_to_terms());
assert!(!settings.data.banned());
assert!(!settings.data.random_other_flag());

settings.data.set_agreed_to_terms(true);
assert!(settings.data.admin());
assert!(settings.data.agreed_to_terms());
assert!(!settings.data.banned());
assert!(!settings.data.random_other_flag());

settings.data.set_banned(true);
assert!(settings.data.admin());
assert!(settings.data.agreed_to_terms());
assert!(settings.data.banned());
assert!(!settings.data.random_other_flag());

settings.data.set_random_other_flag(true);
assert!(settings.data.admin());
assert!(settings.data.agreed_to_terms());
assert!(settings.data.banned());
assert!(settings.data.random_other_flag());

Limitations

The bitwise_boolean macro currently supports up to 8 flags per struct.

License

This project is licensed under the MIT License.

Commit count: 3

cargo fmt