Skip to content

Commit

Permalink
Add write_bytes_only feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Aug 22, 2023
1 parent 0a75a2c commit 2450cf4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ logging = ["deku_derive/logging", "log"]
const_generics = []
## Enable container.read_cache by default when a `Container` is created
read_cache = []
## Disable writing bits, adding performance to writing bytes
##
## This is especially important in order to inline writer::write_bytes(..) and DekuWriter::to_writer(..)
## whenever possible
write_bytes_only = []

[dependencies]
deku_derive = { version = "^0.16.0", path = "deku-derive", default-features = false}
Expand Down
4 changes: 2 additions & 2 deletions src/impls/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ macro_rules! ImplDekuWrite {

// Only have `endian`, return all input
impl DekuWrite<Endian> for $typ {
#[inline(always)]
#[inline]
fn write(
&self,
output: &mut BitVec<u8, Msb0>,
Expand All @@ -663,7 +663,7 @@ macro_rules! ImplDekuWrite {
}

impl DekuWriter<Endian> for $typ {
#[inline(always)]
#[inline]
fn to_writer<W: Write>(
&self,
writer: &mut Writer<W>,
Expand Down
22 changes: 15 additions & 7 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,25 @@ impl<W: Write> Writer<W> {
pub fn write_bytes(&mut self, buf: &[u8]) -> Result<(), DekuError> {
#[cfg(feature = "logging")]
log::trace!("writing {} bytes", buf.len());
if !self.leftover.is_empty() {
#[cfg(feature = "logging")]
log::trace!("leftover exists");
// TODO: we could check here and only send the required bits to finish the byte?
// (instead of sending the entire thing)
self.write_bits(&mut BitVec::from_slice(buf))?;
} else {

if cfg!(feature = "write_bytes_only") {
if let Err(_) = self.inner.write_all(buf) {
return Err(DekuError::WriteError);
}
self.bits_written = buf.len() * 8;
} else {
if self.leftover.is_empty() {
if let Err(_) = self.inner.write_all(buf) {
return Err(DekuError::WriteError);
}
self.bits_written = buf.len() * 8;
} else {
#[cfg(feature = "logging")]
log::trace!("leftover exists");
// TODO: we could check here and only send the required bits to finish the byte?
// (instead of sending the entire thing)
self.write_bits(&mut BitVec::from_slice(buf))?;
}
}

Ok(())
Expand Down

0 comments on commit 2450cf4

Please sign in to comment.