Skip to content

Make mod structure less insane #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
resolver = "3"
members = [
"src",
"src/common",
"src/memory",
"src/memory/endianness",
"src/memory/bitstream",
"src/memory/bitvacuumer",
"src/memory/bitstreamcache",
]

[workspace.package]
Expand Down
15 changes: 15 additions & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "rawspeed-common"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
documentation.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]

[lib]
path = "mod.rs"
81 changes: 81 additions & 0 deletions src/common/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//------------------------------------------------------------------------------

pub trait Integer {}
impl Integer for u8 {}
impl Integer for u16 {}
impl Integer for u32 {}
impl Integer for u64 {}

pub trait Bitwidth {
const BITWIDTH: usize;
}

macro_rules! impl_bitwidth {
($($t:ty)+) => {
$(
impl Bitwidth for $t {
const BITWIDTH: usize = <$t>::BITS as usize;
}
)+
};
}

impl_bitwidth!(u8 u16 u32 u64);

pub trait ConstZero {
const ZERO: Self;
}

impl ConstZero for u8 {
const ZERO: Self = 0;
}
impl ConstZero for u16 {
const ZERO: Self = 0;
}
impl ConstZero for u32 {
const ZERO: Self = 0;
}
impl ConstZero for u64 {
const ZERO: Self = 0;
}

//------------------------------------------------------------------------------

pub fn extract_high_bits<
T: Integer + ConstZero + Bitwidth + std::ops::Shr<usize, Output = T>,
>(
value: T,
num_bits: usize,
) -> T {
if num_bits == 0 {
return <T>::ZERO;
}
assert!(num_bits <= T::BITWIDTH);
let num_low_bits_to_skip = T::BITWIDTH - num_bits;
assert!(num_low_bits_to_skip < T::BITWIDTH);
value >> num_low_bits_to_skip
}

pub fn extract_low_bits<
T: Integer
+ ConstZero
+ Bitwidth
+ std::ops::Shl<usize, Output = T>
+ std::ops::Shr<usize, Output = T>,
>(
value: T,
num_bits: usize,
) -> T {
if num_bits == 0 {
return <T>::ZERO;
}
assert!(num_bits <= T::BITWIDTH);
let num_high_padding_bits = T::BITWIDTH - num_bits;
assert!(num_high_padding_bits < T::BITWIDTH);
(value << num_high_padding_bits) >> num_high_padding_bits
}

//------------------------------------------------------------------------------

#[cfg(test)]
mod test;
Loading