use core::fmt;
use std::collections::TryReserveError;
use std::io::{
Error,
ErrorKind,
Read,
Result,
Seek,
SeekFrom,
};
use qubit_io::{
Buffer,
BufferedInput,
Input,
Seekable,
UncheckedSlice,
};
use crate::{
Codec,
DecodeLifecycleOutput,
DecodeLifecycleProgress,
TranscodeFailure,
TranscodeProgress,
TranscodeStatus,
Transcoder,
codec::assert_unit_bounds,
};
use super::codec_decode_driver::CodecDecodeDriver;
use super::transcode_progress_validation::validate_decode_progress;
pub struct TranscodeDecodeInput<I>
where
I: Input,
I::Item: Copy + Default,
{
input: BufferedInput<I>,
}
impl<I> TranscodeDecodeInput<I>
where
I: Input,
I::Item: Copy + Default,
{
#[must_use]
pub fn new(inner: I) -> Self {
Self {
input: BufferedInput::new(inner),
}
}
#[must_use]
pub fn with_capacity(inner: I, capacity: usize) -> Self {
Self {
input: BufferedInput::with_capacity(inner, capacity),
}
}
#[inline]
pub fn try_with_capacity(
inner: I,
capacity: usize,
) -> std::result::Result<Self, TryReserveError> {
Ok(Self {
input: BufferedInput::try_with_capacity(inner, capacity)?,
})
}
#[must_use]
pub const fn inner(&self) -> &I {
self.input.inner()
}
#[must_use]
pub fn unread_len(&self) -> usize {
self.input.unread_len()
}
#[must_use]
pub fn unread(&self) -> &[I::Item] {
self.input.unread()
}
#[must_use]
pub fn capacity(&self) -> usize {
self.input.capacity()
}
pub fn fill_until(&mut self, count: usize) -> std::io::Result<bool> {
if count > self.input.capacity() {
self.input
.try_reserve_capacity(count)
.map_err(allocation_to_io_error)?;
}
self.input.fill_until(count)
}
pub fn transcode_step<D, M, Value>(
&mut self,
decoder: &mut D,
map_error: &mut M,
output: &mut [Value],
output_index: usize,
count: usize,
) -> Result<Option<TranscodeProgress>>
where
D: Transcoder<Input = I::Item, Output = Value>,
M: FnMut(D::Error) -> Error,
{
let output_end = UncheckedSlice::checked_range_end(
output.len(),
output_index,
count,
"decoded output range exceeds destination buffer",
)?;
if count == 0 {
return Ok(Some(TranscodeProgress::complete(0, 0)));
}
if self.unread_len() == 0 && !self.fill_more()? {
return Ok(None);
}
let available_input = self.unread_len();
let progress = decoder
.transcode(
self.unread(),
0,
&mut output[..output_end],
output_index,
)
.map_err(&mut *map_error)
.and_then(|progress| {
validate_decode_progress(
progress,
0,
available_input,
output_index,
count,
)
})?;
self.consume(progress.read());
Ok(Some(progress))
}
pub fn consume(&mut self, count: usize) {
assert!(
count <= self.unread_len(),
"cannot consume beyond buffered input",
);
unsafe {
self.input.consume(count);
}
}
pub unsafe fn copy_unread_to(
&self,
output: &mut [I::Item],
output_index: usize,
count: usize,
) {
let unread = self.unread();
unsafe {
UncheckedSlice::copy_nonoverlapping(
unread,
0,
output,
output_index,
count,
);
}
}
#[must_use = "the returned input and unread buffer must be handled"]
pub fn into_parts(self) -> (I, Buffer<I::Item>) {
self.input.into_parts()
}
pub unsafe fn read_unchecked(
&mut self,
output: &mut [I::Item],
output_index: usize,
count: usize,
) -> Result<usize> {
debug_assert!(
UncheckedSlice::range_fits(output.len(), output_index, count),
"unchecked read output range exceeds destination buffer",
);
if count == 0 {
return Ok(0);
}
unsafe { self.input.read_unchecked(output, output_index, count) }
}
pub fn read_decoded_with<C, M>(
&mut self,
codec: &mut C,
map_error: M,
) -> Result<C::Value>
where
C: Codec<Unit = I::Item>,
M: FnMut(C::DecodeError) -> Error,
{
TranscodeFailure::ensure_no_decode_lifecycle_output::<C>()
.map_err(|error| Error::new(ErrorKind::Unsupported, error))?;
let progress = self.read_decoded_lifecycle_with_scratch_impl(
codec,
&mut [],
&mut [],
map_error,
)?;
let (value, reset_written, finish_written) = progress.into_parts();
debug_assert_eq!(0, reset_written);
debug_assert_eq!(0, finish_written);
Ok(value)
}
pub fn read_decoded_lifecycle_with<C, M>(
&mut self,
codec: &mut C,
map_error: M,
) -> Result<DecodeLifecycleOutput<C::Value>>
where
C: Codec<Unit = I::Item>,
C::Value: Default,
M: FnMut(C::DecodeError) -> Error,
{
assert_unit_bounds::<C>();
let mut reset_output = Vec::new();
reset_output.resize_with(C::MAX_DECODE_RESET_VALUES, C::Value::default);
let mut finish_output = Vec::new();
finish_output
.resize_with(C::MAX_DECODE_FINISH_VALUES, C::Value::default);
let progress = self.read_decoded_lifecycle_with_scratch_impl(
codec,
&mut reset_output,
&mut finish_output,
map_error,
)?;
let (value, reset_written, finish_written) = progress.into_parts();
reset_output.truncate(reset_written);
finish_output.truncate(finish_written);
Ok(DecodeLifecycleOutput::new(
reset_output,
value,
finish_output,
))
}
pub fn read_decoded_lifecycle_with_scratch<C, M>(
&mut self,
codec: &mut C,
reset_output: &mut [C::Value],
finish_output: &mut [C::Value],
map_error: M,
) -> Result<DecodeLifecycleProgress<C::Value>>
where
C: Codec<Unit = I::Item>,
M: FnMut(C::DecodeError) -> Error,
{
self.read_decoded_lifecycle_with_scratch_impl(
codec,
reset_output,
finish_output,
map_error,
)
}
fn read_decoded_lifecycle_with_scratch_impl<C, M>(
&mut self,
codec: &mut C,
reset_output: &mut [C::Value],
finish_output: &mut [C::Value],
mut map_error: M,
) -> Result<DecodeLifecycleProgress<C::Value>>
where
C: Codec<Unit = I::Item>,
M: FnMut(C::DecodeError) -> Error,
{
assert_unit_bounds::<C>();
if reset_output.len() < C::MAX_DECODE_RESET_VALUES {
return Err(Error::new(
ErrorKind::InvalidInput,
"decode reset output is shorter than the codec reset bound",
));
}
if finish_output.len() < C::MAX_DECODE_FINISH_VALUES {
return Err(Error::new(
ErrorKind::InvalidInput,
"decode finish output is shorter than the codec finish bound",
));
}
let reset_written = unsafe {
codec.decode_reset(reset_output, 0)
}
.map_err(&mut map_error)?;
assert!(
reset_written <= C::MAX_DECODE_RESET_VALUES,
"Codec::decode_reset wrote beyond its reset bound",
);
let value = CodecDecodeDriver::new(&mut self.input)
.read_one(codec, &mut map_error)?;
let finish_written = unsafe {
codec.decode_finish(finish_output, 0)
}
.map_err(&mut map_error)?;
assert!(
finish_written <= C::MAX_DECODE_FINISH_VALUES,
"Codec::decode_finish wrote beyond its finish bound",
);
Ok(DecodeLifecycleProgress::new(
value,
reset_written,
finish_written,
))
}
pub fn reset<D, M, Value>(
&self,
decoder: &mut D,
map_error: &mut M,
output: &mut [Value],
output_index: usize,
count: usize,
) -> Result<usize>
where
D: Transcoder<Input = I::Item, Output = Value>,
M: FnMut(D::Error) -> Error,
{
let required = decoder
.max_reset_output_len()
.map_err(capacity_to_io_error)?;
let output_end = UncheckedSlice::checked_range_end(
output.len(),
output_index,
count,
"reset output range exceeds destination buffer",
)?;
if count < required {
return Err(Error::new(
ErrorKind::InvalidData,
"insufficient output for decoder reset bound",
));
}
let output = &mut output[..output_end];
let written = decoder
.reset(output, output_index)
.map_err(&mut *map_error)?;
assert!(written <= required, "reset wrote beyond its bound");
Ok(written)
}
pub fn transcode<D, M, Value>(
&mut self,
decoder: &mut D,
map_error: &mut M,
output: &mut [Value],
output_index: usize,
count: usize,
) -> Result<usize>
where
D: Transcoder<Input = I::Item, Output = Value>,
M: FnMut(D::Error) -> Error,
{
let output_end = UncheckedSlice::checked_range_end(
output.len(),
output_index,
count,
"decoded output range exceeds destination buffer",
)?;
if count == 0 {
return Ok(0);
}
let output = &mut output[..output_end];
let mut written_total = 0;
loop {
if self.unread_len() == 0 && !self.fill_more()? {
return Ok(written_total);
}
let units = self.unread();
let available_input = units.len();
let remaining_output = count - written_total;
let progress = decoder
.transcode(units, 0, output, output_index + written_total)
.map_err(&mut *map_error)?;
let progress = validate_decode_progress(
progress,
0,
available_input,
output_index + written_total,
remaining_output,
)?;
let consumed = progress.read();
let written = progress.written();
self.consume(consumed);
written_total += written;
match progress.status() {
TranscodeStatus::Complete => {
if written_total == count {
return Ok(written_total);
}
}
TranscodeStatus::NeedOutput { .. } => {
return Ok(written_total);
}
TranscodeStatus::NeedInput { required, .. } => {
if self.fill_until(required.get())? {
continue;
}
return Ok(written_total);
}
}
}
}
pub fn finish<D, M, Value>(
&self,
decoder: &mut D,
map_error: &mut M,
output: &mut [Value],
output_index: usize,
count: usize,
) -> Result<usize>
where
D: Transcoder<Input = I::Item, Output = Value>,
M: FnMut(D::Error) -> Error,
{
let required = decoder
.max_finish_output_len()
.map_err(capacity_to_io_error)?;
let output_end = UncheckedSlice::checked_range_end(
output.len(),
output_index,
count,
"finish output range exceeds destination buffer",
)?;
if count < required {
return Err(Error::new(
ErrorKind::InvalidData,
"insufficient output for decoder finish bound",
));
}
let output = &mut output[..output_end];
let written = decoder
.finish(output, output_index)
.map_err(&mut *map_error)?;
assert!(written <= required, "finish wrote beyond its bound");
Ok(written)
}
}
impl<I> TranscodeDecodeInput<I>
where
I: Input<Item = u8> + Seekable<Unit = u8>,
{
pub fn seek(&mut self, position: SeekFrom) -> Result<u64> {
self.input.seek_to(position)
}
}
impl<I> TranscodeDecodeInput<I>
where
I: Input,
I::Item: Copy + Default,
{
fn fill_more(&mut self) -> Result<bool> {
self.input.fill_more()
}
}
impl<I> Read for TranscodeDecodeInput<I>
where
I: Input<Item = u8>,
{
fn read(&mut self, output: &mut [u8]) -> Result<usize> {
unsafe { self.read_unchecked(output, 0, output.len()) }
}
}
impl<I> Seek for TranscodeDecodeInput<I>
where
I: Input<Item = u8> + Seekable<Unit = u8>,
{
fn seek(&mut self, position: SeekFrom) -> Result<u64> {
self.seek(position)
}
}
impl<I> fmt::Debug for TranscodeDecodeInput<I>
where
I: Input,
I::Item: Copy + Default,
BufferedInput<I>: fmt::Debug,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("TranscodeDecodeInput")
.field("input", &self.input)
.finish()
}
}
fn capacity_to_io_error(error: crate::CapacityError) -> Error {
Error::new(ErrorKind::InvalidData, error)
}
fn allocation_to_io_error(error: std::collections::TryReserveError) -> Error {
Error::new(ErrorKind::OutOfMemory, error)
}