Skip to content

Implement ByteVacuumer #29

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 1 commit into from
Jul 1, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"src/memory/bitstreamcache",
"src/memory/bitstreamer",
"src/memory/variable_length_load",
"src/memory/bytevacuumer",
]

[workspace.package]
Expand Down
19 changes: 19 additions & 0 deletions src/memory/bytevacuumer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "rawspeed-memory-bytevacuumer"
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

[lints]
workspace = true

[dependencies]
rawspeed-memory-endianness = { path = "../endianness" }

[lib]
path = "mod.rs"
106 changes: 106 additions & 0 deletions src/memory/bytevacuumer/bytevacuumer/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use rawspeed_memory_endianness::endianness::Endianness;
use rawspeed_memory_endianness::endianness::SwapBytes;
use rawspeed_memory_endianness::endianness::get_host_endianness;

pub trait ToBits {
type Output;
fn to_bits(self) -> Self::Output;
}

macro_rules! impl_to_bits {
($src:ty, $tgt:ty) => {
impl ToBits for $src {
type Output = $tgt;
fn to_bits(self) -> Self::Output {
Self::Output::from_ne_bytes(self.to_ne_bytes())
}
}
};
}

impl_to_bits!(u8, u8);
impl_to_bits!(i8, u8);
impl_to_bits!(u16, u16);
impl_to_bits!(i16, u16);
impl_to_bits!(u32, u32);
impl_to_bits!(i32, u32);
impl_to_bits!(u64, u64);
impl_to_bits!(i64, u64);

impl_to_bits!(f32, u32);
impl_to_bits!(f64, u64);

pub trait ToNeBytes {
type Output;
fn to_ne_bytes(self) -> Self::Output;
}

impl ToNeBytes for u8 {
type Output = [u8; 1];

fn to_ne_bytes(self) -> Self::Output {
self.to_ne_bytes()
}
}

impl ToNeBytes for u16 {
type Output = [u8; 2];

fn to_ne_bytes(self) -> Self::Output {
self.to_ne_bytes()
}
}

impl ToNeBytes for u32 {
type Output = [u8; 4];

fn to_ne_bytes(self) -> Self::Output {
self.to_ne_bytes()
}
}

impl ToNeBytes for u64 {
type Output = [u8; 8];

fn to_ne_bytes(self) -> Self::Output {
self.to_ne_bytes()
}
}

pub struct ByteVacuumer<'a, W>
where
W: std::io::Write,
{
writer: &'a mut W,
endianness: Endianness,
}

impl<'a, W> ByteVacuumer<'a, W>
where
W: std::io::Write,
{
#[allow(dead_code)]
pub const fn new(writer: &'a mut W, endianness: Endianness) -> Self {
Self { writer, endianness }
}

#[allow(dead_code)]
fn write<T>(&mut self, val: T) -> std::io::Result<()>
where
T: ToBits,
<T as ToBits>::Output: SwapBytes + ToNeBytes,
<<T as ToBits>::Output as ToNeBytes>::Output:
core::ops::Index<core::ops::RangeFull, Output = [u8]>,
{
let val = val.to_bits();
let val =
val.get_byte_swapped(get_host_endianness() != self.endianness);
let bytes = val.to_ne_bytes();
self.writer.write_all(&bytes[..])
}
}

#[cfg(test)]
#[allow(clippy::large_stack_frames)]
#[allow(clippy::cast_sign_loss)]
mod tests;
Loading