Crates.io | bitwise_boolean |
lib.rs | bitwise_boolean |
version | 0.1.0 |
source | src |
created_at | 2024-06-20 23:08:54.961812 |
updated_at | 2024-06-20 23:08:54.961812 |
description | A Rust macro to generate bitwise boolean getters and setters for a struct with u8 field storing up to 8 flags. |
homepage | |
repository | https://github.com/SolWorks-Dev/bitwise_boolean |
max_upload_size | |
id | 1278876 |
size | 6,572 |
A Rust macro to generate bitwise boolean getters and setters for a struct.
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());
The bitwise_boolean
macro currently supports up to 8 flags per struct.
This project is licensed under the MIT License.