pub struct TranscodeEncodeOutput<O>{ /* private fields */ }Expand description
Encodes an Output value stream into an Output unit stream.
This type owns only the unit-level qubit_io::BufferedOutput. Callers
pass a crate::Codec and error mapper to each encode operation, which
lets one buffered output drive different encoders without nesting buffers or
storing codec-specific state in the buffer owner.
Self::flush only drains already buffered units. State-aware streaming
encoders can use Self::reset, Self::transcode, and Self::finish
explicitly.
§Type Parameters
O- Wrapped unit output.
Implementations§
Source§impl<O> TranscodeEncodeOutput<O>
impl<O> TranscodeEncodeOutput<O>
Sourcepub fn with_capacity(inner: O, capacity: usize) -> Self
pub fn with_capacity(inner: O, capacity: usize) -> Self
Sourcepub fn try_with_capacity(
inner: O,
capacity: usize,
) -> Result<Self, TryReserveError>
pub fn try_with_capacity( inner: O, capacity: usize, ) -> Result<Self, TryReserveError>
Sourcepub const fn inner(&self) -> &O
pub const fn inner(&self) -> &O
Returns a shared reference to the wrapped unit output.
Pending units remain in this adapter’s internal buffer and are not
visible through the returned output until Self::flush succeeds.
§Returns
A shared reference to the wrapped unit output.
Sourcepub fn spare_capacity(&self) -> usize
pub fn spare_capacity(&self) -> usize
Returns the available capacity of the spare output buffer.
§Returns
The number of output units that can still be appended without flushing.
Sourcepub fn spare_raw_parts_mut(&mut self) -> (&mut [O::Item], usize, usize)
pub fn spare_raw_parts_mut(&mut self) -> (&mut [O::Item], usize, usize)
Returns raw spare-buffer parts for the internal output buffer.
§Returns
The full backing storage, the spare start index, and the spare unit count.
Sourcepub unsafe fn advance(&mut self, count: usize)
pub unsafe fn advance(&mut self, count: usize)
Marks count units from Self::spare_raw_parts_mut as written.
§Safety
The caller must guarantee that count <= Self::spare_capacity() and
that the corresponding units in the returned spare slice have been
initialized.
Sourcepub fn ensure_spare_capacity(&mut self, count: usize) -> Result<()>
pub fn ensure_spare_capacity(&mut self, count: usize) -> Result<()>
Ensures that at least count spare units are available.
§Parameters
count- Number of spare units required.
§Errors
Returns allocation errors mapped to ErrorKind::OutOfMemory, or I/O
errors from the wrapped output while flushing pending units.
Sourcepub fn into_parts(self) -> (O, Buffer<O::Item>)
pub fn into_parts(self) -> (O, Buffer<O::Item>)
Consumes this adapter without flushing the wrapped output.
This method does not call Self::flush and performs no I/O. Pending
units remain in the returned buffer, which transfers their delivery
responsibility to the caller.
§Returns
The wrapped output and the buffer holding pending units.
Sourcepub fn flush(&mut self) -> Result<()>
pub fn flush(&mut self) -> Result<()>
Flushes buffered units without finishing any encoder stream.
§Errors
Returns errors from the wrapped output while flushing pending units.
Sourcepub fn write_encoded_with<C, M>(
&mut self,
codec: &mut C,
value: &C::Value,
map_error: M,
) -> Result<()>
pub fn write_encoded_with<C, M>( &mut self, codec: &mut C, value: &C::Value, map_error: M, ) -> Result<()>
Encodes one codec value into this buffered unit output.
The method grows the persistent internal buffer when necessary, then writes the complete encoded value into its spare window.
§Parameters
codec- Codec used for this single-value encode.value- Value to encode.map_error- Mapper for codec-domain encode errors.
§Errors
Returns I/O errors from the wrapped output, InvalidInput when the
codec output bound overflows or the value is outside the codec domain,
or the error returned by map_error for codec encode, reset, and finish
failures.
§Panics
Panics when the codec violates its declared reset, value-width, or
finish bounds, or when encode writes a length different from
encode_len in the same reset state.
Sourcepub fn reset<E, M, Value>(
&mut self,
encoder: &mut E,
map_error: &mut M,
) -> Result<()>
pub fn reset<E, M, Value>( &mut self, encoder: &mut E, map_error: &mut M, ) -> Result<()>
Runs encoder reset and buffers any stream-prefix units.
This method reserves enough persistent buffer capacity for all reset
output before calling encoder. It does not flush pending units.
§Errors
Returns capacity errors, allocation errors, or reset errors mapped by
map_error.
§Panics
Panics when encoder writes more than its declared reset bound.
Sourcepub fn transcode<E, M, Value>(
&mut self,
encoder: &mut E,
map_error: &mut M,
input: &[Value],
input_index: usize,
count: usize,
) -> Result<usize>
pub fn transcode<E, M, Value>( &mut self, encoder: &mut E, map_error: &mut M, input: &[Value], input_index: usize, count: usize, ) -> Result<usize>
Encodes values from an indexed input range using a streaming
Transcoder.
This streaming path writes directly into the internal spare buffer and
grows it for any larger NeedOutput.required value reported by
encoder.
§Parameters
encoder- Streaming encoder used for this operation.map_error- Function mapping transcode errors into I/O errors.input- Source values.input_index- Start index insideinput.count- Maximum number of values to encode.
§Returns
The number of source values consumed.
§Errors
Returns invalid input ranges, capacity, transcode, or output errors.
§Examples
A value that only implements Transcoder is not an encoder and cannot
be passed to this method.
use qubit_codec::{
CapacityError, TranscodeEncodeError, TranscodeEncodeOutput,
TranscodeProgress, Transcoder,
};
struct GenericTranscoder;
impl Transcoder for GenericTranscoder {
type Input = u8;
type Output = u8;
type Error = TranscodeEncodeError<(), u8>;
fn max_transcode_output_len(
&self,
input_len: usize,
) -> Result<usize, CapacityError> {
Ok(input_len)
}
fn reset(
&mut self,
_output: &mut [u8],
_output_index: usize,
) -> Result<usize, Self::Error> {
Ok(0)
}
fn transcode(
&mut self,
input: &[u8],
input_index: usize,
_output: &mut [u8],
_output_index: usize,
) -> Result<TranscodeProgress, Self::Error> {
Ok(TranscodeProgress::complete(input.len() - input_index, 0))
}
fn finish(
&mut self,
_output: &mut [u8],
_output_index: usize,
) -> Result<usize, Self::Error> {
Ok(0)
}
}
let mut output = TranscodeEncodeOutput::with_capacity(std::io::sink(), 1);
let mut transcoder = GenericTranscoder;
let mut map_error = |_| std::io::Error::other("transcode error");
let _ = output.transcode(
&mut transcoder,
&mut map_error,
&[0_u8],
0,
1,
);Sourcepub fn finish<E, M, Value>(
&mut self,
encoder: &mut E,
map_error: &mut M,
) -> Result<()>
pub fn finish<E, M, Value>( &mut self, encoder: &mut E, map_error: &mut M, ) -> Result<()>
Finishes the encoder and flushes the wrapped unit output.
This method writes final units directly into the internal spare buffer,
growing it when Transcoder::max_finish_output_len exceeds the
current capacity.
§Parameters
encoder- Encoder whose final units are being collected.map_error- Function mapping transcode errors into I/O errors.
§Errors
Returns capacity, transcode finalization, or wrapped output flush errors.
Sourcepub fn finish_to_buffer<E, M, Value>(
&mut self,
encoder: &mut E,
map_error: &mut M,
) -> Result<()>
pub fn finish_to_buffer<E, M, Value>( &mut self, encoder: &mut E, map_error: &mut M, ) -> Result<()>
Finishes the encoder while retaining final units in the output buffer.
This method separates encoder finalization from output delivery. It is
useful when a caller must record successful finalization before a later
flush can fail. Call Self::flush to deliver the retained units.
§Parameters
encoder- Encoder whose final units are being collected.map_error- Function mapping transcode errors into I/O errors.
§Errors
Returns capacity planning, allocation, or transcoder finalization errors. It does not perform output I/O.
§Panics
Panics when encoder writes more units than its declared finish bound.
Source§impl<O> TranscodeEncodeOutput<O>
impl<O> TranscodeEncodeOutput<O>
Trait Implementations§
Source§impl<O> Debug for TranscodeEncodeOutput<O>
impl<O> Debug for TranscodeEncodeOutput<O>
Source§impl<O> Seek for TranscodeEncodeOutput<O>
impl<O> Seek for TranscodeEncodeOutput<O>
Source§fn seek(&mut self, position: SeekFrom) -> Result<u64>
fn seek(&mut self, position: SeekFrom) -> Result<u64>
Flushes pending bytes, then seeks the wrapped byte output.
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)Source§impl<O> Write for TranscodeEncodeOutput<O>
impl<O> Write for TranscodeEncodeOutput<O>
Source§fn write(&mut self, input: &[u8]) -> Result<usize>
fn write(&mut self, input: &[u8]) -> Result<usize>
Writes raw bytes through the internal buffer.
Source§fn write_all(&mut self, input: &[u8]) -> Result<()>
fn write_all(&mut self, input: &[u8]) -> Result<()>
Writes all raw bytes through the internal buffer.
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)Auto Trait Implementations§
impl<O> Freeze for TranscodeEncodeOutput<O>
impl<O> RefUnwindSafe for TranscodeEncodeOutput<O>
impl<O> Send for TranscodeEncodeOutput<O>
impl<O> Sync for TranscodeEncodeOutput<O>
impl<O> Unpin for TranscodeEncodeOutput<O>
impl<O> UnsafeUnpin for TranscodeEncodeOutput<O>
impl<O> UnwindSafe for TranscodeEncodeOutput<O>
Blanket Implementations§
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
Source§impl<W> Output for W
impl<W> Output for W
Source§unsafe fn write_unchecked(
&mut self,
input: &[u8],
index: usize,
count: usize,
) -> Result<usize, Error>
unsafe fn write_unchecked( &mut self, input: &[u8], index: usize, count: usize, ) -> Result<usize, Error>
Writes bytes from a caller-validated indexed input range.
§Parameters
input: Source byte slice.index: Start offset insideinput.count: Maximum number of bytes to write.
§Returns
The number of bytes reported by the standard writer.
§Errors
Returns the error reported by the standard writer.
§Panics
Panics in debug builds if the requested source range does not fit.
§Safety
The caller must ensure that index through index plus count is a valid range inside input without overflowing.
Source§fn write(&mut self, input: &[<W as Output>::Item]) -> Result<usize, Error>
fn write(&mut self, input: &[<W as Output>::Item]) -> Result<usize, Error>
Writes bytes from the complete input slice.
§Parameters
input: Source byte slice.
§Returns
The number of bytes written from input.
§Errors
Returns std::io::ErrorKind::InvalidData if the writer reports more
bytes than input contains, or the error reported by the standard
writer.
Source§fn is_buffered(&self) -> bool
fn is_buffered(&self) -> bool
Source§impl<T> WriteExt for T
impl<T> WriteExt for T
impl<T> WriteSeek for T
Source§impl<T> WriteSeekExt for T
impl<T> WriteSeekExt for T
Source§fn write_all_at_preserving_position(
&mut self,
offset: u64,
buffer: &[u8],
) -> Result<(), Error>
fn write_all_at_preserving_position( &mut self, offset: u64, buffer: &[u8], ) -> Result<(), Error>
Writes at an absolute offset and restores the original position.
§Parameters
offset: Absolute byte offset to write at.buffer: Bytes to write.
§Returns
Ok(()) after all bytes have been written and the position restored.
§Errors
Returns an error when querying, changing, or restoring the stream position, or when writing fails.