Skip to main content

qubit_codec/transcode/
encode_plan.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! One-value encoding plan used by buffered encoder hooks.
9
10/// Describes how much output capacity one encoded value needs before writing.
11///
12/// `EncodePlan` is produced by [`crate::TranscodeEncodeHooks::prepare_encode`]
13/// and consumed by [`crate::TranscodeEncodeHooks::write_encode`]. The capacity
14/// field is a safe upper bound required by the concrete writer, not necessarily
15/// the exact number of units that will be written.
16///
17/// # Type Parameters
18///
19/// - `A`: Concrete action interpreted by the encoder implementation.
20#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
21pub struct EncodePlan<A> {
22    /// Output units that must be writable before calling `write_encode`.
23    ///
24    /// Default codec-backed encoders use [`crate::Codec::encode_len`] for the
25    /// current value. Domain-specific encoders may use another safe bound,
26    /// such as a charset encoder using its exact encoded length probe. A
27    /// value of zero is valid for policies that consume input without
28    /// producing output.
29    pub max_output_units: usize,
30
31    /// Concrete write action interpreted by the encoder implementation.
32    pub action: A,
33}
34
35impl<A> EncodePlan<A> {
36    /// Creates an encoding plan.
37    ///
38    /// # Parameters
39    ///
40    /// - `max_output_units`: Output capacity required before writing.
41    /// - `action`: Concrete plan action for the encoder implementation.
42    ///
43    /// # Returns
44    ///
45    /// Returns an encoding plan carrying the supplied capacity bound and
46    /// action.
47    #[must_use]
48    #[inline(always)]
49    pub const fn new(max_output_units: usize, action: A) -> Self {
50        Self {
51            max_output_units,
52            action,
53        }
54    }
55}