Skip to content

Commit 2450cf4

Browse files
committed
Add write_bytes_only feature
1 parent 0a75a2c commit 2450cf4

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ logging = ["deku_derive/logging", "log"]
2727
const_generics = []
2828
## Enable container.read_cache by default when a `Container` is created
2929
read_cache = []
30+
## Disable writing bits, adding performance to writing bytes
31+
##
32+
## This is especially important in order to inline writer::write_bytes(..) and DekuWriter::to_writer(..)
33+
## whenever possible
34+
write_bytes_only = []
3035

3136
[dependencies]
3237
deku_derive = { version = "^0.16.0", path = "deku-derive", default-features = false}

src/impls/primitive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ macro_rules! ImplDekuWrite {
647647

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

665665
impl DekuWriter<Endian> for $typ {
666-
#[inline(always)]
666+
#[inline]
667667
fn to_writer<W: Write>(
668668
&self,
669669
writer: &mut Writer<W>,

src/writer.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,25 @@ impl<W: Write> Writer<W> {
5656
pub fn write_bytes(&mut self, buf: &[u8]) -> Result<(), DekuError> {
5757
#[cfg(feature = "logging")]
5858
log::trace!("writing {} bytes", buf.len());
59-
if !self.leftover.is_empty() {
60-
#[cfg(feature = "logging")]
61-
log::trace!("leftover exists");
62-
// TODO: we could check here and only send the required bits to finish the byte?
63-
// (instead of sending the entire thing)
64-
self.write_bits(&mut BitVec::from_slice(buf))?;
65-
} else {
59+
60+
if cfg!(feature = "write_bytes_only") {
6661
if let Err(_) = self.inner.write_all(buf) {
6762
return Err(DekuError::WriteError);
6863
}
6964
self.bits_written = buf.len() * 8;
65+
} else {
66+
if self.leftover.is_empty() {
67+
if let Err(_) = self.inner.write_all(buf) {
68+
return Err(DekuError::WriteError);
69+
}
70+
self.bits_written = buf.len() * 8;
71+
} else {
72+
#[cfg(feature = "logging")]
73+
log::trace!("leftover exists");
74+
// TODO: we could check here and only send the required bits to finish the byte?
75+
// (instead of sending the entire thing)
76+
self.write_bits(&mut BitVec::from_slice(buf))?;
77+
}
7078
}
7179

7280
Ok(())

0 commit comments

Comments
 (0)