qubit-codec 0.17.4

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.
// =============================================================================
//! Outcome of one buffered decode attempt.

use core::num::NonZeroUsize;

/// Outcome produced by a decode-side engine for one source attempt.
///
/// This type describes what happened to source input after the engine has
/// decoded, applied invalid-input hooks, and optionally delivered a logical
/// value to its caller-provided consumer. It does not describe target output
/// capacity for conversion pipelines; downstream encode backpressure is
/// reported separately by the encode side.
#[must_use]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) enum DecodeOutcome {
    /// A logical value was emitted to the decode consumer.
    Emitted {
        /// Source units consumed for the emitted value.
        read: NonZeroUsize,
        /// Logical values emitted by this decode attempt.
        emitted: NonZeroUsize,
    },

    /// Source input was consumed without emitting a value.
    Skipped {
        /// Source units consumed by the skip.
        read: NonZeroUsize,
    },

    /// More source input is required before decoding can continue.
    NeedInput {
        /// Total source units required from the current input position.
        required: NonZeroUsize,
    },
}

impl DecodeOutcome {
    /// Creates an emitted-value outcome.
    ///
    /// # Parameters
    ///
    /// - `read`: Nonzero source units consumed.
    /// - `emitted`: Nonzero logical values delivered to the consumer.
    ///
    /// # Returns
    ///
    /// Returns `Emitted` with both counters preserved.
    #[inline]
    pub(crate) const fn emitted(read: NonZeroUsize, emitted: NonZeroUsize) -> Self {
        Self::Emitted { read, emitted }
    }

    /// Creates a skipped-input outcome.
    ///
    /// # Parameters
    ///
    /// - `read`: Nonzero source units discarded without emitting values.
    ///
    /// # Returns
    ///
    /// Returns `Skipped` with the consumed-unit count.
    #[inline]
    pub(crate) const fn skipped(read: NonZeroUsize) -> Self {
        Self::Skipped { read }
    }

    /// Creates a missing-input outcome.
    ///
    /// # Parameters
    ///
    /// - `required`: Total source units needed at the current input position.
    ///
    /// # Returns
    ///
    /// Returns `NeedInput` with the required total, not an additional-unit
    /// count.
    #[inline]
    pub(crate) const fn need_input(required: NonZeroUsize) -> Self {
        Self::NeedInput { required }
    }
}