jenslog-rs/src/lib.rs

50 lines
1.2 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;
///gets the flag at the position of `flag`. `flag` must have exactly 1 bit set
fn get_flag(val: u32, flag: u32) -> bool {
if has_one_bit_set(flag) {
val & flag != 0
} else {
panic!("flag must have 1 bit set")
}
}
///true if exactly 1 bit of the given number is set
fn has_one_bit_set(n: u32) -> bool {
//Dont ask me why this works! its mathgic!
n > 0 && n & (n - 1) == 0
}
#[cfg(test)]
mod tests {
#[test]
fn get_flag() {
assert!(super::get_flag(5, 1 << 2));
assert!(!super::get_flag(3, 1 << 2));
}
#[test]
#[should_panic]
fn get_flag_panic() {
super::get_flag(0, 3);
}
#[test]
fn has_one_bit_set() {
assert!(super::has_one_bit_set(1));
assert!(!super::has_one_bit_set(3));
}
}