Struct variable_size_byte_writer::VariableSizeByteWriter
[−]
[src]
pub struct VariableSizeByteWriter<W> where
W: Write, { /* fields omitted */ }
VariableSizeByteWriter
provides functionality 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. The internal buffer is
flushed when the object is dropped but any errors that occur during the flushing
go unhandled. Manual flushing is therefore recommended.
Examples
Writing some unconventionally sized bytes into Vec<u8>
:
use variable_size_byte_writer::*; let mut target = Vec::new(); let mut writer = VariableSizeByteWriter::new(target); let bytes = [(0x3F, 6),(0x1AFF, 13),(0x7, 3)]; bytes .iter() .for_each(|&(byte, bits)| writer.write::<Max16>(byte, bits).unwrap() );
Writing a series of 7-bit bytes into a file, manually flushing the internal buffer and capturing the required bits to pad the last byte:
use std::fs::File; use variable_size_byte_writer::*; let mut file = File::create("path").unwrap(); let mut writer = VariableSizeByteWriter::new(file); for variable in 0..0x8F { writer.write::<Max8>(variable, 7).unwrap(); } let mut padding = 0; writer.flush(&mut padding).unwrap();
Methods
impl<W> VariableSizeByteWriter<W> where
W: Write,
[src]
W: Write,
fn new(target: W) -> Self
[src]
Creates a new instance of VariableSizeByteWriter
.
The function takes a io::Write
traited object target
as an argument.
Examples
use std::fs::File; use variable_size_byte_writer::*; let mut file = File::create("path").unwrap(); let mut writer = VariableSizeByteWriter::new(file);
fn with_capacity(target: W, cap: usize) -> Self
[src]
Creates a new instance of VariableSizeByteWriter
with non default internal buffer size.
The function takes buffer capacity cap
and io::Write
traited
object target
as arguments.
Examples
use std::fs::File; use variable_size_byte_writer::*; let mut file = File::create("path").unwrap(); let mut writer = VariableSizeByteWriter::with_capacity(file, 4096);
fn write<M>(&mut self, variable: u64, bits: u32) -> Result<()> where
M: ByteSize,
[src]
M: ByteSize,
Writes a variable-sized byte variable
with a specific length of bits
into the given target
.
The function uses the generic or 'turbofish' syntax to optimize the
write call withing 8-bit boundaries from Max8
to Max64
. The intent
is to use the minimum boundary that fits the selected use case.
The padding of the variable must be clean as in all zeroes.
The function might return an error 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 file = File::create("path")?; let mut writer = VariableSizeByteWriter::new(file); writer.write::<Max40>(0x71CFFABFF, 35)?;
fn flush(&mut self, padding: &mut u32) -> Result<()>
[src]
Flushes the remaining internal buffer.
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 file = File::create("path")?; let mut writer = VariableSizeByteWriter::new(file); writer.write::<Max16>(0xAF, 9)?; writer.write::<Max16>(0x1A4, 11)?; writer.write::<Max16>(0x7B, 8)?; let mut padding = 0; writer.flush(&mut padding)?;
Trait Implementations
impl<W> Drop for VariableSizeByteWriter<W> where
W: Write,
[src]
W: Write,