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.
// =============================================================================
//! Slot that owns the converter's retained decoded value.

use super::super::engine::TranscodeEncodeEngine;
use super::super::engine::TranscodeEncodeHooks;
use super::pending_value::PendingValue;
use crate::CapacityError;
use crate::Codec;

/// Slot that owns the converter's retained decoded value.
///
/// # Type Parameters
///
/// - `Value`: Decoded value retained until the target encoder has capacity.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(in crate::transcode) struct PendingValueSlot<Value> {
    /// Retained decoded value waiting for output capacity.
    value: Option<PendingValue<Value>>,
}

impl<Value> PendingValueSlot<Value> {
    /// Creates an empty pending-value slot.
    ///
    /// # Returns
    ///
    /// Returns a slot with no retained value.
    #[must_use]
    #[inline]
    pub(in crate::transcode) const fn empty() -> Self {
        Self { value: None }
    }

    /// Returns the current target-output bound for the retained value.
    ///
    /// Unlike public transcoder capacity methods, this helper intentionally
    /// examines transient state so a concrete finish call can accept the
    /// capacity required by its current pending value.
    ///
    /// # Type Parameters
    ///
    /// - `E`: Encoder codec type used to query output bounds.
    /// - `H`: Encoder hook type used by the encoder engine.
    ///
    /// # Parameters
    ///
    /// - `engine`: Target encode engine for one-value output planning.
    ///
    /// # Returns
    ///
    /// Returns zero for an empty slot, or the encoder bound for one value.
    ///
    /// # Errors
    ///
    /// Returns a capacity error when encoder planning overflows.
    #[inline]
    pub(in crate::transcode) fn current_output_len<E, H>(
        &self,
        engine: &TranscodeEncodeEngine<E, H>,
    ) -> Result<usize, CapacityError>
    where
        E: Codec<Value = Value>,
        H: TranscodeEncodeHooks<E>,
    {
        if self.value.is_some() {
            engine.max_transcode_output_len(1)
        } else {
            Ok(0)
        }
    }

    /// Stores a decoded value that could not be encoded yet.
    ///
    /// # Parameters
    ///
    /// - `pending`: Decoded value and its source input position.
    #[inline]
    pub(in crate::transcode) fn put(&mut self, pending: PendingValue<Value>) {
        self.value = Some(pending);
    }

    /// Takes the retained decoded value, if any.
    ///
    /// # Returns
    ///
    /// Returns the retained value when present, otherwise `None`.
    #[inline]
    pub(in crate::transcode) fn take(&mut self) -> Option<PendingValue<Value>> {
        self.value.take()
    }

    /// Removes any retained decoded value.
    ///
    /// # Returns
    ///
    /// Returns unit `()`.
    #[inline]
    pub(in crate::transcode) fn clear(&mut self) {
        self.value = None;
    }
}