jenslog-rs/src/lib.rs

61 lines
1.4 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;
//TODO generify this?
impl FlagOperations for u32 {
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")
}
}
}
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!(5u32.get_flag(1 << 2));
assert!(!3u32.get_flag(1 << 2));
}
#[test]
#[should_panic]
fn get_flag_panic() {
0u32.get_flag(3);
}
#[test]
fn has_one_bit_set() {
assert!(1u32.has_one_bit_set());
assert!(!3u32.has_one_bit_set());
}
}