Struct variable_size_byte_writer::VariableSizeByteWriter
[−]
[src]
pub struct VariableSizeByteWriter { /* fields omitted */ }VariableSizeByteWriter provides functions for writing variable-size bytes
into io::Write traited targets.
Writes are internally buffered and so the usage of any additional buffering
such as std::io::BufWriter is not recommended.
Note that VariableSizeByteWriter does not flush its internal buffer when
dropped.
Examples
Writing some unconventionally sized bytes into Vec<u8>
use variable_size_byte_writer::*; let mut target = Vec::new(); let mut writer = VariableSizeByteWriter::new(); let bytes = [(0x3F, 6),(0x1AFF, 13),(0x7, 3)]; bytes .iter() .for_each(|&(byte, bits)| writer.write_16(&mut target, byte, bits).unwrap() ); let mut padding = 0; writer .flush_all_bytes(&mut target, &mut padding) .unwrap(); assert_eq!(padding, 2); assert_eq!(target[..], [0xFF, 0xBF, 0x3E]);
Writing a series of 7bit bytes into a file
use std::fs::File; use variable_size_byte_writer::*; let mut writer = VariableSizeByteWriter::new(); let mut file = File::create("path").unwrap(); for variable in 0..0x8F { writer.write_8(&mut file, variable, 7).unwrap(); } let mut padding = 0; writer .flush_all_bytes(&mut file, &mut padding) .unwrap();
Methods
impl VariableSizeByteWriter[src]
fn new() -> Self[src]
Creates a new instance of VariableSizeByteWriter with a default
internal buffer size.
Examples
use variable_size_byte_writer::*; let writer = VariableSizeByteWriter::new();
fn with_specified_capacity(cap: usize) -> Self[src]
Creates a new instance of VariableSizeByteWriter with a specific
internal buffer size.
Examples
use variable_size_byte_writer::*; let writer = VariableSizeByteWriter::with_specified_capacity(4096);
fn write_64<T>(
&mut self,
writer: &mut T,
variable: u64,
bits: u32
) -> Result<()> where
T: Write, [src]
&mut self,
writer: &mut T,
variable: u64,
bits: u32
) -> Result<()> where
T: Write,
Writes a variable-sized byte variable with a specific length of bits
into the given target.
As with all the write functions, the operation is buffered and the
buffer must eventually be flushed with the flush_all_bytes function.
The given byte can be no longer than 64 bits. The padding of the variable must be clean as in all zeroes.
The function might fail once the internal buffer fills up and is flushed into the given target.
Examples
use std::fs::File; use variable_size_byte_writer::*; let mut writer = VariableSizeByteWriter::with_specified_capacity(4096); let mut file = File::create("path")?; writer.write_64(&mut file, 0x71CFFABFF, 35)?;
fn write_32<T>(
&mut self,
writer: &mut T,
variable: u32,
bits: u32
) -> Result<()> where
T: Write, [src]
&mut self,
writer: &mut T,
variable: u32,
bits: u32
) -> Result<()> where
T: Write,
Faster than write_64 but the given byte can be no longer than 32 bits.
fn write_16<T>(
&mut self,
writer: &mut T,
variable: u16,
bits: u32
) -> Result<()> where
T: Write, [src]
&mut self,
writer: &mut T,
variable: u16,
bits: u32
) -> Result<()> where
T: Write,
Faster than write_32 but the given byte can be no longer than 16 bits.
fn write_8<T>(&mut self, writer: &mut T, variable: u8, bits: u32) -> Result<()> where
T: Write, [src]
T: Write,
Faster than write_16 but the given byte can be no longer than 8 bits.
fn flush_all_bytes<T>(
&mut self,
writer: &mut T,
padding: &mut u32
) -> Result<()> where
T: Write, [src]
&mut self,
writer: &mut T,
padding: &mut u32
) -> Result<()> where
T: Write,
Flushes the remaining internal buffer to the given target.
The function might fail, successfully flushing none or some of the internal buffer. If the flush fails, the internal buffer remains intact and contains the content that failed to flush.
The padding required to fill the last partial byte can be captured
into the argument padding.
The padding is only valid if the function return without an error.
Examples
use std::fs::File; use variable_size_byte_writer::*; let mut writer = VariableSizeByteWriter::with_specified_capacity(4096); let mut file = File::create("path")?; writer.write_64(&mut file, 0x71CFFABFF, 35)?; writer.write_64(&mut file, 0xFFA, 16)?; writer.write_64(&mut file, 0xF1CFFABCD, 39)?; let mut padding = 0; writer.flush_all_bytes(&mut file, &mut padding)?;