jenslog-rs/src/lib.rs

66 lines
1.7 KiB
Rust

//region Clippy config
#![warn(clippy::pedantic)]
//disable silly rules
#![allow(
clippy::module_name_repetitions, //complains about function names which makes no sense, so disabled
clippy::must_use_candidate, //no i dont want to add the must_use attribute to everything
clippy::cast_lossless, clippy::cast_possible_wrap, //lossy casts are required to work with garbage WinApi
)]
//endregion
pub mod logging;
pub mod key;
macro_rules! impl_flag_operations {
($($ty:ty),*) => {
$(
impl FlagOperations for $ty {
fn has_one_bit_set(&self) -> bool {
*self > 0 && *self & (*self - 1) == 0
}
fn get_flag(&self, flag: Self) -> bool {
if flag.has_one_bit_set() {
self & flag != 0
} else {
panic!("flag must have 1 bit set")
}
}
}
)*
};
}
impl_flag_operations!(usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128);
trait FlagOperations {
///true if exactly 1 bit of the given number is set
fn has_one_bit_set(&self) -> bool;
///gets the flag at the position of `flag`. `flag` must have exactly 1 bit set
/// `flag` should have 1 bit set at the position of the flag to get
fn get_flag(&self, flag: Self) -> bool;
}
#[cfg(test)]
mod tests {
use crate::FlagOperations;
#[test]
fn get_flag() {
assert!(5.get_flag(1 << 2));
assert!(!3.get_flag(1 << 2));
}
#[test]
#[should_panic]
fn get_flag_panic() {
0.get_flag(3);
}
#[test]
fn has_one_bit_set() {
assert!(1.has_one_bit_set());
assert!(!3.has_one_bit_set());
}
}