Move the bit types to tool_helper and merge them

This commit is contained in:
Jaby 2023-04-20 21:01:27 +02:00 committed by Jaby
parent 50605520b4
commit 5d47718a8d
4 changed files with 39 additions and 50 deletions

View File

@ -1,42 +0,0 @@
pub struct BitRange {
start: usize,
length: usize
}
impl BitRange {
pub const fn from_to(start_bit: usize, end_bit: usize) -> Self {
Self{start: start_bit, length: (end_bit - start_bit) + 1}
}
pub const fn get_mask(&self) -> usize {
self.max_value()
}
pub const fn max_value(&self) -> usize {
(1 << self.length) - 1
}
pub const fn or_value(&self, dst_value: usize, value: usize) -> usize {
dst_value | ((value & self.get_mask()) << self.start)
}
}
pub struct Bit {
pos: usize
}
impl Bit {
pub const fn at(pos: usize) -> Self {
Bit{pos}
}
pub const fn or_value(&self, dst_value: usize, is_set: bool) -> usize {
if is_set {
dst_value | (1 << self.pos)
}
else {
dst_value
}
}
}

View File

@ -1,4 +1,3 @@
pub mod bits;
pub (super) mod helper; pub (super) mod helper;
pub mod layout; pub mod layout;
pub mod file_map; pub mod file_map;

View File

@ -1,8 +1,8 @@
use super::{bits::{Bit, BitRange}, layout::Layout, File, FileSystemMap}; use super::{layout::Layout, File, FileSystemMap};
use super::super::encoder::LengthCalculatorFunction; use super::super::encoder::LengthCalculatorFunction;
use std::path::PathBuf; use std::path::PathBuf;
use no_comment::{IntoWithoutComments as _, languages}; use no_comment::{IntoWithoutComments as _, languages};
use tool_helper::{Error, format_if_error, read_file, read_file_to_string, format_if_error_drop_cause}; use tool_helper::{bits::{Bit, BitRange}, Error, format_if_error, read_file, read_file_to_string, format_if_error_drop_cause};
pub type LBANameVec = Vec<String>; pub type LBANameVec = Vec<String>;

View File

@ -1,11 +1,43 @@
pub struct BitRange { pub struct BitRange {
pub start: usize, start: usize,
pub len: usize, length: usize
} }
impl BitRange { impl BitRange {
pub const fn from_to(start: usize, end: usize) -> BitRange { pub const fn from_to(start_bit: usize, end_bit: usize) -> Self {
BitRange{start, len: (end - start + 1)} Self{start: start_bit, length: (end_bit - start_bit) + 1}
}
pub const fn get_mask(&self) -> usize {
self.max_value()
}
pub const fn max_value(&self) -> usize {
(1 << self.length) - 1
}
pub const fn or_value(&self, dst_value: usize, value: usize) -> usize {
dst_value | ((value & self.get_mask()) << self.start)
}
}
pub struct Bit {
pos: usize
}
impl Bit {
pub const fn at(pos: usize) -> Self {
Bit{pos}
}
pub const fn or_value(&self, dst_value: usize, is_set: bool) -> usize {
if is_set {
dst_value | (1 << self.pos)
}
else {
dst_value
}
} }
} }
@ -13,7 +45,7 @@ macro_rules! create_bit_functions {
($type_val:ty) => { ($type_val:ty) => {
paste::item! { paste::item! {
const fn [< get_mask_ $type_val >](range: &BitRange) -> $type_val { const fn [< get_mask_ $type_val >](range: &BitRange) -> $type_val {
(1 << range.len) - 1 range.get_mask() as $type_val
} }
pub const fn [< clear_value_ $type_val >](dst: $type_val, range: &BitRange) -> $type_val { pub const fn [< clear_value_ $type_val >](dst: $type_val, range: &BitRange) -> $type_val {