fix vec trail
This commit is contained in:
commit
731428f798
6 changed files with 126 additions and 0 deletions
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "binf"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
description = "A crate that adds utilities for dealing with binary flags"
|
||||
authors = ["0David <me@davidon.top>"]
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/D0A1V2I3D/binf"
|
||||
documentation = "https://docs.rs/binf/"
|
||||
|
||||
[dependencies]
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2022 0David
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
documentation and better readme coming soon
|
||||
|
||||
any contribution (bug report, issue, feature request, pull request) is welcome
|
16
src/flag_utils.rs
Normal file
16
src/flag_utils.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
pub fn get_flags(flags: i32) -> Vec<i32> {
|
||||
let mut fvec: Vec<bool> = Vec::new();
|
||||
let mut rest = flags;
|
||||
while rest != 0 {
|
||||
fvec.push((rest % 2) != 0);
|
||||
rest = rest / 2;
|
||||
}
|
||||
|
||||
let mut tvec: Vec<i32> = Vec::new();
|
||||
for (i, value) in fvec.iter().enumerate() {
|
||||
if *value {
|
||||
tvec.push(i as i32);
|
||||
}
|
||||
}
|
||||
tvec
|
||||
}
|
59
src/lib.rs
Normal file
59
src/lib.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*!
|
||||
TODO documentation
|
||||
*/
|
||||
|
||||
pub mod flag_utils;
|
||||
pub mod macros;
|
||||
|
||||
///struct that holds data for a flag and useful functions
|
||||
pub struct Flag {
|
||||
value: u128,
|
||||
}
|
||||
|
||||
impl Flag {
|
||||
/// set a flag value
|
||||
pub fn set_flag(&mut self, flag: u32, value: bool) {
|
||||
if value {
|
||||
self.value |= 1 << flag;
|
||||
} else {
|
||||
if self.get_flag(flag) {
|
||||
self.value -= 1 << flag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// get a value of flag
|
||||
pub fn get_flag(&self, flag: u32) -> bool {
|
||||
(self.value & (1 << flag)) != 0
|
||||
}
|
||||
|
||||
/// returns a vector with all enabled flags
|
||||
pub fn get_all_flags(&self) -> Vec<bool> {
|
||||
let mut ret = vec![];
|
||||
for i in 0..128 {
|
||||
ret.push(&self.value & (1u128 << i) != 0);
|
||||
}
|
||||
for i in (0..ret.len()).rev() {
|
||||
if ret[i] {
|
||||
ret.truncate(i + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
/// creates Flag from int
|
||||
pub fn new_from_value(value: u128) -> Self {
|
||||
Self {value}
|
||||
}
|
||||
|
||||
/// returns flags value
|
||||
pub fn get(&self) -> u128 {
|
||||
self.value
|
||||
}
|
||||
|
||||
/// initializes a Flag with 0, use flag_new! macro to create with flags instead
|
||||
pub fn new() -> Self {
|
||||
Self { value: 0, }
|
||||
}
|
||||
}
|
15
src/macros.rs
Normal file
15
src/macros.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
//#[macro_use]
|
||||
/// macro that creates new Flag
|
||||
/// for example: flag_new![3, 7] makes a flag that has those flags enabled (136)
|
||||
#[macro_export]
|
||||
macro_rules! flag_new {
|
||||
( $($f:expr),* ) => {
|
||||
{
|
||||
let mut tflag = crate::Flag::new();
|
||||
$(
|
||||
tflag.set_flag($f, true);
|
||||
)*
|
||||
tflag
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue