fixes and utils

This commit is contained in:
D0A1V2I3D 2023-02-05 15:42:57 +01:00
parent 731428f798
commit 1ae1341cab
No known key found for this signature in database
GPG key ID: 70C7C44D1C3EC16F
3 changed files with 22 additions and 5 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "binf" name = "binf"
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
description = "A crate that adds utilities for dealing with binary flags" description = "A crate that adds utilities for dealing with binary flags"

View file

@ -13,4 +13,14 @@ pub fn get_flags(flags: i32) -> Vec<i32> {
} }
} }
tvec tvec
}
pub fn vec_to_flag(vec: Vec<bool>) -> u128 {
let mut flag = 0;
for (i, &val) in vec.iter().enumerate() {
if val {
flag |= 1 << i;
}
}
flag
} }

View file

@ -4,6 +4,8 @@ TODO documentation
pub mod flag_utils; pub mod flag_utils;
pub mod macros; pub mod macros;
#[cfg(test)]
mod test;
///struct that holds data for a flag and useful functions ///struct that holds data for a flag and useful functions
pub struct Flag { pub struct Flag {
@ -16,9 +18,7 @@ impl Flag {
if value { if value {
self.value |= 1 << flag; self.value |= 1 << flag;
} else { } else {
if self.get_flag(flag) { self.value &= !(1 << flag);
self.value -= 1 << flag;
}
} }
} }
@ -31,7 +31,7 @@ impl Flag {
pub fn get_all_flags(&self) -> Vec<bool> { pub fn get_all_flags(&self) -> Vec<bool> {
let mut ret = vec![]; let mut ret = vec![];
for i in 0..128 { for i in 0..128 {
ret.push(&self.value & (1u128 << i) != 0); ret.push(&self.value & (1 << i) != 0);
} }
for i in (0..ret.len()).rev() { for i in (0..ret.len()).rev() {
if ret[i] { if ret[i] {
@ -42,6 +42,8 @@ impl Flag {
ret ret
} }
/// creates Flag from int /// creates Flag from int
pub fn new_from_value(value: u128) -> Self { pub fn new_from_value(value: u128) -> Self {
Self {value} Self {value}
@ -52,6 +54,11 @@ impl Flag {
self.value self.value
} }
/// sets internal value to this
pub fn set_value(&mut self, value: u128) {
self.value = value;
}
/// initializes a Flag with 0, use flag_new! macro to create with flags instead /// initializes a Flag with 0, use flag_new! macro to create with flags instead
pub fn new() -> Self { pub fn new() -> Self {
Self { value: 0, } Self { value: 0, }