fixes and utils
This commit is contained in:
parent
731428f798
commit
1ae1341cab
3 changed files with 22 additions and 5 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "binf"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
description = "A crate that adds utilities for dealing with binary flags"
|
||||
|
|
|
@ -14,3 +14,13 @@ pub fn get_flags(flags: i32) -> Vec<i32> {
|
|||
}
|
||||
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
|
||||
}
|
15
src/lib.rs
15
src/lib.rs
|
@ -4,6 +4,8 @@ TODO documentation
|
|||
|
||||
pub mod flag_utils;
|
||||
pub mod macros;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
///struct that holds data for a flag and useful functions
|
||||
pub struct Flag {
|
||||
|
@ -16,9 +18,7 @@ impl Flag {
|
|||
if value {
|
||||
self.value |= 1 << flag;
|
||||
} 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> {
|
||||
let mut ret = vec![];
|
||||
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() {
|
||||
if ret[i] {
|
||||
|
@ -42,6 +42,8 @@ impl Flag {
|
|||
ret
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// creates Flag from int
|
||||
pub fn new_from_value(value: u128) -> Self {
|
||||
Self {value}
|
||||
|
@ -52,6 +54,11 @@ impl Flag {
|
|||
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
|
||||
pub fn new() -> Self {
|
||||
Self { value: 0, }
|
||||
|
|
Loading…
Reference in a new issue