mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-04 00:54:07 +02:00
flexible error types for reserve
This commit is contained in:
parent
973f543e1b
commit
716d32fecb
2 changed files with 13 additions and 8 deletions
|
|
@ -695,8 +695,8 @@ pub struct LazyBitRead<'a, T: BitRead<'a, E>, E: Endianness> {
|
||||||
impl<'a, T: BitRead<'a, E>, E: Endianness> LazyBitRead<'a, T, E> {
|
impl<'a, T: BitRead<'a, E>, E: Endianness> LazyBitRead<'a, T, E> {
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Get the contents of the lazy struct
|
/// Get the contents of the lazy struct
|
||||||
pub fn read(mut self) -> Result<T> {
|
pub fn read(&self) -> Result<T> {
|
||||||
self.source.read::<T>()
|
self.source.clone().read::<T>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -306,11 +306,11 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write the length of a section before the section
|
/// Write the length of a section before the section
|
||||||
pub fn reserve_length<F: Fn(&mut BitWriteStream<E>) -> Result<()>>(
|
pub fn reserve_length<Err: From<BitError>, F: Fn(&mut BitWriteStream<E>) -> Result<(), Err>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
length_bit_size: usize,
|
length_bit_size: usize,
|
||||||
body_fn: F,
|
body_fn: F,
|
||||||
) -> Result<()> {
|
) -> Result<(), Err> {
|
||||||
let (head, tail) = self.buffer.reserve(length_bit_size);
|
let (head, tail) = self.buffer.reserve(length_bit_size);
|
||||||
let mut head = BitWriteStream { buffer: head };
|
let mut head = BitWriteStream { buffer: head };
|
||||||
let mut tail = BitWriteStream { buffer: tail };
|
let mut tail = BitWriteStream { buffer: tail };
|
||||||
|
|
@ -319,15 +319,19 @@ where
|
||||||
body_fn(&mut tail)?;
|
body_fn(&mut tail)?;
|
||||||
let end = tail.bit_len();
|
let end = tail.bit_len();
|
||||||
let bit_len = end - start;
|
let bit_len = end - start;
|
||||||
head.write_sized(&bit_len, length_bit_size)
|
head.write_sized(&bit_len, length_bit_size)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write the length in bytes of a section before the section, the section will be 0 padded to an even byte length
|
/// Write the length in bytes of a section before the section, the section will be 0 padded to an even byte length
|
||||||
pub fn reserve_byte_length<F: Fn(&mut BitWriteStream<E>) -> Result<()>>(
|
pub fn reserve_byte_length<
|
||||||
|
Err: From<BitError>,
|
||||||
|
F: Fn(&mut BitWriteStream<E>) -> Result<(), Err>,
|
||||||
|
>(
|
||||||
&mut self,
|
&mut self,
|
||||||
length_bit_size: usize,
|
length_bit_size: usize,
|
||||||
body_fn: F,
|
body_fn: F,
|
||||||
) -> Result<()> {
|
) -> Result<(), Err> {
|
||||||
let (head, tail) = self.buffer.reserve(length_bit_size);
|
let (head, tail) = self.buffer.reserve(length_bit_size);
|
||||||
let mut head = BitWriteStream { buffer: head };
|
let mut head = BitWriteStream { buffer: head };
|
||||||
let mut tail = BitWriteStream { buffer: tail };
|
let mut tail = BitWriteStream { buffer: tail };
|
||||||
|
|
@ -342,6 +346,7 @@ where
|
||||||
|
|
||||||
let byte_len = (bit_len + pad_len) / 8;
|
let byte_len = (bit_len + pad_len) / 8;
|
||||||
|
|
||||||
head.write_sized(&byte_len, length_bit_size)
|
head.write_sized(&byte_len, length_bit_size)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue