mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-04 00:54:07 +02:00
add reserve_int
This commit is contained in:
parent
c321c5a252
commit
227582cbc4
3 changed files with 35 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "bitbuffer"
|
||||
version = "0.9.8"
|
||||
version = "0.9.9"
|
||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||
edition = "2018"
|
||||
description = "Reading bit sequences from a byte slice"
|
||||
|
|
@ -21,4 +21,8 @@ iai = "0.1"
|
|||
name = "bench"
|
||||
harness = false
|
||||
|
||||
[[bench]]
|
||||
name = "write"
|
||||
harness = false
|
||||
|
||||
[workspace]
|
||||
|
|
|
|||
15
benches/write.rs
Normal file
15
benches/write.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
use bitbuffer::{BitWriteStream, LittleEndian};
|
||||
use iai::black_box;
|
||||
|
||||
fn write_int_le() {
|
||||
let mut out = Vec::with_capacity(128);
|
||||
{
|
||||
let mut write = BitWriteStream::new(&mut out, LittleEndian);
|
||||
for i in 0..128 {
|
||||
write.write_sized(&black_box(i), 7).unwrap();
|
||||
}
|
||||
}
|
||||
black_box(out);
|
||||
}
|
||||
|
||||
iai::main!(write_int_le);
|
||||
|
|
@ -349,4 +349,19 @@ where
|
|||
head.write_sized(&byte_len, length_bit_size)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reserve the length to write an integer
|
||||
pub fn reserve_int<Err: From<BitError>, F: Fn(&mut BitWriteStream<E>) -> Result<u64, Err>>(
|
||||
&mut self,
|
||||
length_bit_size: usize,
|
||||
body_fn: F,
|
||||
) -> Result<(), Err> {
|
||||
let (head, tail) = self.buffer.reserve(length_bit_size);
|
||||
let mut head = BitWriteStream { buffer: head };
|
||||
let mut tail = BitWriteStream { buffer: tail };
|
||||
|
||||
let head_int = body_fn(&mut tail)?;
|
||||
head.write_sized(&head_int, length_bit_size)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue