pub struct BinaryWriter<W, O = BigEndian> { /* private fields */ }Expand description
Writer wrapper for fixed-width binary values.
The byte order is selected by the O type parameter. Use
BinaryWriter<W, BigEndian> for big-endian data and
BinaryWriter<W, LittleEndian> for little-endian data.
§Type Parameters
W: Underlying byte output.O: Compile-time byte-order specification.
§Partial writes
These methods are not transactional. An output error may occur after the output has accepted a byte prefix of the encoded value, and those bytes are not rolled back. Calling a method again encodes the entire value from the output’s current position and may duplicate that prefix. Recovery requires outer framing, a trusted seek or rollback strategy, or a transactional transport. Output acceptance does not imply durable persistence.
§Examples
use qubit_codec::LittleEndian;
use qubit_io_binary::BinaryWriter;
let mut writer = BinaryWriter::<_, LittleEndian>::new(Vec::new());
writer.write_u16(0x1234).expect("value should encode");
assert_eq!(vec![0x34, 0x12], writer.into_inner());Implementations§
Source§impl<W, O> BinaryWriter<W, O>where
O: ByteOrderSpec,
impl<W, O> BinaryWriter<W, O>where
O: ByteOrderSpec,
Sourcepub const fn byte_order(&self) -> ByteOrder
pub const fn byte_order(&self) -> ByteOrder
Returns the byte order selected by this writer.
§Returns
Returns the compile-time byte order as a runtime value.
Sourcepub fn inner_mut(&mut self) -> &mut W
pub fn inner_mut(&mut self) -> &mut W
Returns an exclusive reference to the underlying writer.
§Returns
Returns mutable access to the wrapped writer.
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Source§impl<W> BinaryWriter<W, BigEndian>where
W: ByteOutput,
impl<W> BinaryWriter<W, BigEndian>where
W: ByteOutput,
Sourcepub fn write_u128(&mut self, value: u128) -> Result<()>
pub fn write_u128(&mut self, value: u128) -> Result<()>
Sourcepub fn write_i128(&mut self, value: i128) -> Result<()>
pub fn write_i128(&mut self, value: i128) -> Result<()>
Sourcepub fn write_string_with_u16_len(&mut self, value: &str) -> Result<()>
pub fn write_string_with_u16_len(&mut self, value: &str) -> Result<()>
Writes a UTF-8 string prefixed by a 16-bit byte length.
§Parameters
value: String to length-prefix and write.
§Returns
Returns after the length and payload have been written.
§Errors
Returns an invalid-input error when the length exceeds
u16::MAX, or an output error while writing.
§Partial writes
This operation is not transactional. An I/O error may leave the length prefix and part of the payload already accepted by the output. Retrying this whole method can write a second prefix; recover using the enclosing protocol’s framing or transaction boundary instead.
Sourcepub fn write_string_with_u32_len(&mut self, value: &str) -> Result<()>
pub fn write_string_with_u32_len(&mut self, value: &str) -> Result<()>
Writes a UTF-8 string prefixed by a 32-bit byte length.
§Parameters
value: String to length-prefix and write.
§Returns
Returns after the length and payload have been written.
§Errors
Returns an invalid-input error when the length exceeds
u32::MAX, or an output error while writing.
§Partial writes
This operation is not transactional. An I/O error may leave the length prefix and part of the payload already accepted by the output. Retrying this whole method can write a second prefix; recover using the enclosing protocol’s framing or transaction boundary instead.
Source§impl<W> BinaryWriter<W, LittleEndian>where
W: ByteOutput,
impl<W> BinaryWriter<W, LittleEndian>where
W: ByteOutput,
Sourcepub fn write_u128(&mut self, value: u128) -> Result<()>
pub fn write_u128(&mut self, value: u128) -> Result<()>
Sourcepub fn write_i128(&mut self, value: i128) -> Result<()>
pub fn write_i128(&mut self, value: i128) -> Result<()>
Sourcepub fn write_string_with_u16_len(&mut self, value: &str) -> Result<()>
pub fn write_string_with_u16_len(&mut self, value: &str) -> Result<()>
Writes a UTF-8 string prefixed by a 16-bit byte length.
§Parameters
value: String to length-prefix and write.
§Returns
Returns after the length and payload have been written.
§Errors
Returns an invalid-input error when the length exceeds
u16::MAX, or an output error while writing.
§Partial writes
This operation is not transactional. An I/O error may leave the length prefix and part of the payload already accepted by the output. Retrying this whole method can write a second prefix; recover using the enclosing protocol’s framing or transaction boundary instead.
Sourcepub fn write_string_with_u32_len(&mut self, value: &str) -> Result<()>
pub fn write_string_with_u32_len(&mut self, value: &str) -> Result<()>
Writes a UTF-8 string prefixed by a 32-bit byte length.
§Parameters
value: String to length-prefix and write.
§Returns
Returns after the length and payload have been written.
§Errors
Returns an invalid-input error when the length exceeds
u32::MAX, or an output error while writing.
§Partial writes
This operation is not transactional. An I/O error may leave the length prefix and part of the payload already accepted by the output. Retrying this whole method can write a second prefix; recover using the enclosing protocol’s framing or transaction boundary instead.
Source§impl<W> BinaryWriter<W, NativeEndian>where
W: ByteOutput,
impl<W> BinaryWriter<W, NativeEndian>where
W: ByteOutput,
Sourcepub fn write_u128(&mut self, value: u128) -> Result<()>
pub fn write_u128(&mut self, value: u128) -> Result<()>
Sourcepub fn write_i128(&mut self, value: i128) -> Result<()>
pub fn write_i128(&mut self, value: i128) -> Result<()>
Sourcepub fn write_string_with_u16_len(&mut self, value: &str) -> Result<()>
pub fn write_string_with_u16_len(&mut self, value: &str) -> Result<()>
Writes a UTF-8 string prefixed by a 16-bit byte length.
§Parameters
value: String to length-prefix and write.
§Returns
Returns after the length and payload have been written.
§Errors
Returns an invalid-input error when the length exceeds
u16::MAX, or an output error while writing.
§Partial writes
This operation is not transactional. An I/O error may leave the length prefix and part of the payload already accepted by the output. Retrying this whole method can write a second prefix; recover using the enclosing protocol’s framing or transaction boundary instead.
Sourcepub fn write_string_with_u32_len(&mut self, value: &str) -> Result<()>
pub fn write_string_with_u32_len(&mut self, value: &str) -> Result<()>
Writes a UTF-8 string prefixed by a 32-bit byte length.
§Parameters
value: String to length-prefix and write.
§Returns
Returns after the length and payload have been written.
§Errors
Returns an invalid-input error when the length exceeds
u32::MAX, or an output error while writing.
§Partial writes
This operation is not transactional. An I/O error may leave the length prefix and part of the payload already accepted by the output. Retrying this whole method can write a second prefix; recover using the enclosing protocol’s framing or transaction boundary instead.
Source§impl<W, O> BinaryWriter<W, O>where
W: ByteOutput,
impl<W, O> BinaryWriter<W, O>where
W: ByteOutput,
Sourcepub fn is_buffered(&self) -> bool
pub fn is_buffered(&self) -> bool
Returns whether this writer buffers output.
§Returns
Returns true when writes may be retained in an internal buffer.
Sourcepub unsafe fn write_unchecked(
&mut self,
input: &[u8],
index: usize,
count: usize,
) -> Result<usize>
pub unsafe fn write_unchecked( &mut self, input: &[u8], index: usize, count: usize, ) -> Result<usize>
Writes up to count bytes from an indexed input range.
§Parameters
input: Source byte buffer.index: Start of the readable range.count: Maximum number of bytes to write.
§Returns
Returns the number of bytes written.
§Errors
Returns the underlying output error.
§Safety
index..index + count must be a valid range within input.
Sourcepub unsafe fn write_fully_unchecked(
&mut self,
input: &[u8],
index: usize,
count: usize,
) -> Result<()>
pub unsafe fn write_fully_unchecked( &mut self, input: &[u8], index: usize, count: usize, ) -> Result<()>
Writes all bytes from an indexed input range.
§Parameters
input: Source byte buffer.index: Start of the readable range.count: Number of bytes to write.
§Errors
Returns the underlying output error if the complete range cannot be written.
§Safety
index..index + count must be a valid range within input.
Sourcepub fn write_fully(&mut self, input: &[u8]) -> Result<()>
pub fn write_fully(&mut self, input: &[u8]) -> Result<()>
Source§impl<W, O> BinaryWriter<W, O>where
W: SeekableByteOutput,
impl<W, O> BinaryWriter<W, O>where
W: SeekableByteOutput,
Sourcepub fn seek_to(&mut self, position: SeekFrom) -> Result<u64>
pub fn seek_to(&mut self, position: SeekFrom) -> Result<u64>
Seeks the wrapped byte stream; see Seekable::seek_to for
the seek contract. Buffered wrappers interpret relative offsets
and report positions relative to their logical stream position.
A position query does not flush buffered output.
§Parameters
position: Relative or absolute target position.
§Returns
Returns the resulting stream position in bytes. For buffered wrappers this is the logical position, including buffered data.
§Errors
Returns the underlying seek error.
Trait Implementations§
Source§impl<W, O> Output for BinaryWriter<W, O>where
W: ByteOutput,
impl<W, O> Output for BinaryWriter<W, O>where
W: ByteOutput,
Source§fn is_buffered(&self) -> bool
fn is_buffered(&self) -> bool
Reports whether the wrapped output is buffered.
§Returns
Returns the wrapped output’s buffering state.
Source§unsafe fn write_unchecked(
&mut self,
input: &[u8],
index: usize,
count: usize,
) -> Result<usize>
unsafe fn write_unchecked( &mut self, input: &[u8], index: usize, count: usize, ) -> Result<usize>
Writes up to count bytes from an indexed input range.
§Parameters
input: Source slice.index: First source index.count: Maximum number of bytes to write.
§Returns
Returns the number of bytes written.
§Errors
Returns an error reported by the wrapped output.
§Safety
index..index + count must be a valid range within input.
Source§fn into_std_write(self) -> StdWrite<Self> ⓘwhere
Self: Sized + ByteOutput,
fn into_std_write(self) -> StdWrite<Self> ⓘwhere
Self: Sized + ByteOutput,
Source§fn write(&mut self, input: &[Self::Item]) -> Result<usize, Error>
fn write(&mut self, input: &[Self::Item]) -> Result<usize, Error>
Source§unsafe fn write_fully_unchecked(
&mut self,
input: &[Self::Item],
index: usize,
count: usize,
) -> Result<(), Error>
unsafe fn write_fully_unchecked( &mut self, input: &[Self::Item], index: usize, count: usize, ) -> Result<(), Error>
Source§impl<W, O> Seekable for BinaryWriter<W, O>where
W: SeekableByteOutput,
impl<W, O> Seekable for BinaryWriter<W, O>where
W: SeekableByteOutput,
Auto Trait Implementations§
impl<W, O> Forget for BinaryWriter<W, O>
impl<W, O> Freeze for BinaryWriter<W, O>
impl<W, O> Move for BinaryWriter<W, O>
impl<W, O> RefUnwindSafe for BinaryWriter<W, O>
impl<W, O> Send for BinaryWriter<W, O>
impl<W, O> Sync for BinaryWriter<W, O>
impl<W, O> Unpin for BinaryWriter<W, O>
impl<W, O> UnsafeUnpin for BinaryWriter<W, O>
impl<W, O> UnwindSafe for BinaryWriter<W, O>
Blanket Implementations§
Source§impl<W> BinaryWriteExt for Wwhere
W: ByteOutput + ?Sized,
impl<W> BinaryWriteExt for Wwhere
W: ByteOutput + ?Sized,
Source§fn write_u16(&mut self, value: u16, byte_order: ByteOrder) -> Result<()>
fn write_u16(&mut self, value: u16, byte_order: ByteOrder) -> Result<()>
Source§fn write_u16_be(&mut self, value: u16) -> Result<()>
fn write_u16_be(&mut self, value: u16) -> Result<()>
Source§fn write_u16_le(&mut self, value: u16) -> Result<()>
fn write_u16_le(&mut self, value: u16) -> Result<()>
Source§fn write_u32(&mut self, value: u32, byte_order: ByteOrder) -> Result<()>
fn write_u32(&mut self, value: u32, byte_order: ByteOrder) -> Result<()>
Source§fn write_u32_be(&mut self, value: u32) -> Result<()>
fn write_u32_be(&mut self, value: u32) -> Result<()>
Source§fn write_u32_le(&mut self, value: u32) -> Result<()>
fn write_u32_le(&mut self, value: u32) -> Result<()>
Source§fn write_u64(&mut self, value: u64, byte_order: ByteOrder) -> Result<()>
fn write_u64(&mut self, value: u64, byte_order: ByteOrder) -> Result<()>
Source§fn write_u64_be(&mut self, value: u64) -> Result<()>
fn write_u64_be(&mut self, value: u64) -> Result<()>
Source§fn write_u64_le(&mut self, value: u64) -> Result<()>
fn write_u64_le(&mut self, value: u64) -> Result<()>
Source§fn write_u128(&mut self, value: u128, byte_order: ByteOrder) -> Result<()>
fn write_u128(&mut self, value: u128, byte_order: ByteOrder) -> Result<()>
Source§fn write_u128_be(&mut self, value: u128) -> Result<()>
fn write_u128_be(&mut self, value: u128) -> Result<()>
Source§fn write_u128_le(&mut self, value: u128) -> Result<()>
fn write_u128_le(&mut self, value: u128) -> Result<()>
Source§fn write_i16(&mut self, value: i16, byte_order: ByteOrder) -> Result<()>
fn write_i16(&mut self, value: i16, byte_order: ByteOrder) -> Result<()>
Source§fn write_i16_be(&mut self, value: i16) -> Result<()>
fn write_i16_be(&mut self, value: i16) -> Result<()>
Source§fn write_i16_le(&mut self, value: i16) -> Result<()>
fn write_i16_le(&mut self, value: i16) -> Result<()>
Source§fn write_i32(&mut self, value: i32, byte_order: ByteOrder) -> Result<()>
fn write_i32(&mut self, value: i32, byte_order: ByteOrder) -> Result<()>
Source§fn write_i32_be(&mut self, value: i32) -> Result<()>
fn write_i32_be(&mut self, value: i32) -> Result<()>
Source§fn write_i32_le(&mut self, value: i32) -> Result<()>
fn write_i32_le(&mut self, value: i32) -> Result<()>
Source§fn write_i64(&mut self, value: i64, byte_order: ByteOrder) -> Result<()>
fn write_i64(&mut self, value: i64, byte_order: ByteOrder) -> Result<()>
Source§fn write_i64_be(&mut self, value: i64) -> Result<()>
fn write_i64_be(&mut self, value: i64) -> Result<()>
Source§fn write_i64_le(&mut self, value: i64) -> Result<()>
fn write_i64_le(&mut self, value: i64) -> Result<()>
Source§fn write_i128(&mut self, value: i128, byte_order: ByteOrder) -> Result<()>
fn write_i128(&mut self, value: i128, byte_order: ByteOrder) -> Result<()>
Source§fn write_i128_be(&mut self, value: i128) -> Result<()>
fn write_i128_be(&mut self, value: i128) -> Result<()>
Source§fn write_i128_le(&mut self, value: i128) -> Result<()>
fn write_i128_le(&mut self, value: i128) -> Result<()>
Source§fn write_f32(&mut self, value: f32, byte_order: ByteOrder) -> Result<()>
fn write_f32(&mut self, value: f32, byte_order: ByteOrder) -> Result<()>
Source§fn write_f32_be(&mut self, value: f32) -> Result<()>
fn write_f32_be(&mut self, value: f32) -> Result<()>
Source§fn write_f32_le(&mut self, value: f32) -> Result<()>
fn write_f32_le(&mut self, value: f32) -> Result<()>
Source§fn write_f64(&mut self, value: f64, byte_order: ByteOrder) -> Result<()>
fn write_f64(&mut self, value: f64, byte_order: ByteOrder) -> Result<()>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> ByteOutput for T
Source§impl<W> Leb128WriteExt for Wwhere
W: ByteOutput + ?Sized,
impl<W> Leb128WriteExt for Wwhere
W: ByteOutput + ?Sized,
Source§fn write_uleb_u16(&mut self, value: u16) -> Result<()>
fn write_uleb_u16(&mut self, value: u16) -> Result<()>
u16. Read moreSource§fn write_uleb_u32(&mut self, value: u32) -> Result<()>
fn write_uleb_u32(&mut self, value: u32) -> Result<()>
u32. Read moreSource§fn write_uleb_u64(&mut self, value: u64) -> Result<()>
fn write_uleb_u64(&mut self, value: u64) -> Result<()>
u64. Read moreSource§fn write_uleb_u128(&mut self, value: u128) -> Result<()>
fn write_uleb_u128(&mut self, value: u128) -> Result<()>
u128. Read moreSource§fn write_uleb_usize(&mut self, value: usize) -> Result<()>
fn write_uleb_usize(&mut self, value: usize) -> Result<()>
usize. Read moreimpl<T> SeekableByteOutput for T
impl<T> SeekableOutput for T
Source§impl<T> StringWriteExt for Twhere
T: ByteOutput + ?Sized,
impl<T> StringWriteExt for Twhere
T: ByteOutput + ?Sized,
Source§fn write_utf8_payload(&mut self, value: &str) -> Result<(), Error>
fn write_utf8_payload(&mut self, value: &str) -> Result<(), Error>
Writes the string’s UTF-8 bytes without adding a length prefix.
A write failure may occur after some payload bytes have reached the underlying output.
§Parameters
value: UTF-8 string whose bytes are written to the output.
§Returns
Returns after all UTF-8 payload bytes have been written.
§Errors
Returns an I/O error from the underlying output.
Source§fn write_utf8_string_uleb_usize(&mut self, value: &str) -> Result<(), Error>
fn write_utf8_string_uleb_usize(&mut self, value: &str) -> Result<(), Error>
Writes the UTF-8 byte length as target-width-dependent unsigned LEB128, followed by the string bytes.
A failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after its target-width-dependent byte length.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns an I/O error from writing the prefix or payload.
Source§fn write_utf8_string_uleb_u64(&mut self, value: &str) -> Result<(), Error>
fn write_utf8_string_uleb_u64(&mut self, value: &str) -> Result<(), Error>
Writes the UTF-8 byte length as unsigned LEB128 u64, then the string
bytes.
Returns InvalidInput if the byte length does not fit in u64; a
write failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after itsu64byte length.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns InvalidInput if the byte length does not fit in u64, or an
I/O error while writing.
Source§fn write_string_with_u16_len(
&mut self,
value: &str,
byte_order: ByteOrder,
) -> Result<(), Error>
fn write_string_with_u16_len( &mut self, value: &str, byte_order: ByteOrder, ) -> Result<(), Error>
Writes a u16 UTF-8 byte-length prefix in the requested byte order,
followed by the string bytes.
Returns InvalidInput if the byte length exceeds u16::MAX; a write
failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after itsu16byte length.byte_order: Byte order used to encode the length prefix.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns InvalidInput if the byte length exceeds u16::MAX, or an
I/O error while writing.
Source§fn write_string_with_u16_len_be(&mut self, value: &str) -> Result<(), Error>
fn write_string_with_u16_len_be(&mut self, value: &str) -> Result<(), Error>
Writes a big-endian u16 UTF-8 byte-length prefix, then the string
bytes.
Returns InvalidInput if the byte length exceeds u16::MAX; a write
failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after its big-endianu16byte length.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns InvalidInput if the byte length exceeds u16::MAX, or an
I/O error while writing.
Source§fn write_string_with_u16_len_le(&mut self, value: &str) -> Result<(), Error>
fn write_string_with_u16_len_le(&mut self, value: &str) -> Result<(), Error>
Writes a little-endian u16 UTF-8 byte-length prefix, then the string
bytes.
Returns InvalidInput if the byte length exceeds u16::MAX; a write
failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after its little-endianu16byte length.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns InvalidInput if the byte length exceeds u16::MAX, or an
I/O error while writing.
Source§fn write_string_with_u32_len(
&mut self,
value: &str,
byte_order: ByteOrder,
) -> Result<(), Error>
fn write_string_with_u32_len( &mut self, value: &str, byte_order: ByteOrder, ) -> Result<(), Error>
Writes a u32 UTF-8 byte-length prefix in the requested byte order,
followed by the string bytes.
Returns InvalidInput if the byte length exceeds u32::MAX; a write
failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after itsu32byte length.byte_order: Byte order used to encode the length prefix.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns InvalidInput if the byte length exceeds u32::MAX, or an
I/O error while writing.
Source§fn write_string_with_u32_len_be(&mut self, value: &str) -> Result<(), Error>
fn write_string_with_u32_len_be(&mut self, value: &str) -> Result<(), Error>
Writes a big-endian u32 UTF-8 byte-length prefix, then the string
bytes.
Returns InvalidInput if the byte length exceeds u32::MAX; a write
failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after its big-endianu32byte length.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns InvalidInput if the byte length exceeds u32::MAX, or an
I/O error while writing.
Source§fn write_string_with_u32_len_le(&mut self, value: &str) -> Result<(), Error>
fn write_string_with_u32_len_le(&mut self, value: &str) -> Result<(), Error>
Writes a little-endian u32 UTF-8 byte-length prefix, then the string
bytes.
Returns InvalidInput if the byte length exceeds u32::MAX; a write
failure can leave a partial prefix or payload in the output.
§Parameters
value: UTF-8 string to write after its little-endianu32byte length.
§Returns
Returns after the length prefix and payload have been written.
§Errors
Returns InvalidInput if the byte length exceeds u32::MAX, or an
I/O error while writing.