qubit-codec 0.12.0

Core codec traits and buffer conversion primitives for Rust
Documentation
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Tests for the semantic transcode converter marker trait.

use qubit_codec::{
    CapacityError,
    TranscodeConvertError,
    TranscodeConverter,
    TranscodeProgress,
    Transcoder,
};

#[derive(Default)]
struct ByteToWord;

impl Transcoder for ByteToWord {
    type Input = u8;
    type Output = u16;
    type Error = TranscodeConvertError<
        core::convert::Infallible,
        core::convert::Infallible,
        u8,
    >;

    fn max_transcode_output_len(
        &self,
        input_len: usize,
    ) -> Result<usize, CapacityError> {
        Ok(input_len)
    }

    fn reset(
        &mut self,
        output: &mut [u16],
        output_index: usize,
    ) -> Result<usize, Self::Error> {
        qubit_codec::TranscodeFailure::ensure_output_index(
            output.len(),
            output_index,
        )?;
        Ok(0)
    }

    fn transcode(
        &mut self,
        input: &[u8],
        input_index: usize,
        output: &mut [u16],
        output_index: usize,
    ) -> Result<TranscodeProgress, Self::Error> {
        let readable = input.len().saturating_sub(input_index);
        let writable = output.len().saturating_sub(output_index);
        let count = readable.min(writable);
        for offset in 0..count {
            output[output_index + offset] = input[input_index + offset] as u16;
        }
        Ok(TranscodeProgress::complete(count, count))
    }

    fn finish(
        &mut self,
        output: &mut [u16],
        output_index: usize,
    ) -> Result<usize, Self::Error> {
        qubit_codec::TranscodeFailure::ensure_output_index(
            output.len(),
            output_index,
        )?;
        Ok(0)
    }
}

impl TranscodeConverter for ByteToWord {
    type DecodeError = core::convert::Infallible;
    type EncodeError = core::convert::Infallible;
    type Value = u8;
}

#[test]
fn test_transcode_converter_is_a_semantic_transcoder_bound() {
    fn assert_converter<T: TranscodeConverter<Input = u8, Output = u16>>() {}

    assert_converter::<ByteToWord>();
}