Skip to main content

qubit_codec/value/
decode_lifecycle_output.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//! Owned output from a complete single-value decode lifecycle.
9
10/// Values produced by decode reset, the main decode, and decode finish.
11///
12/// This result keeps lifecycle output in phase order without allowing finish
13/// output to overwrite reset output.
14///
15/// # Type Parameters
16///
17/// - `V`: Logical value type produced by the codec.
18///
19/// # Examples
20///
21/// ```
22/// use qubit_codec::DecodeLifecycleOutput;
23///
24/// fn inspect<V>(output: &DecodeLifecycleOutput<V>) -> usize {
25///     output.reset().len() + output.finish().len()
26/// }
27/// ```
28#[derive(Clone, Debug, Eq, Hash, PartialEq)]
29#[must_use]
30pub struct DecodeLifecycleOutput<V> {
31    /// Values emitted while resetting decode state.
32    reset: Vec<V>,
33    /// Main value decoded from the supplied input.
34    value: V,
35    /// Values emitted while finishing decode state.
36    finish: Vec<V>,
37}
38
39impl<V> DecodeLifecycleOutput<V> {
40    /// Creates owned output from the three decode lifecycle phases.
41    ///
42    /// # Parameters
43    ///
44    /// - `reset`: Values emitted while resetting decode state.
45    /// - `value`: Main value decoded from the supplied input.
46    /// - `finish`: Values emitted while finishing decode state.
47    ///
48    /// # Returns
49    ///
50    /// Returns output preserving all three lifecycle phases.
51    #[inline(always)]
52    pub(crate) fn new(reset: Vec<V>, value: V, finish: Vec<V>) -> Self {
53        Self { reset, value, finish }
54    }
55
56    /// Returns values emitted while resetting decode state.
57    ///
58    /// # Returns
59    ///
60    /// Returns the reset output in emission order.
61    #[inline(always)]
62    #[must_use]
63    pub fn reset(&self) -> &[V] {
64        &self.reset
65    }
66
67    /// Returns the main value decoded from the supplied input.
68    ///
69    /// # Returns
70    ///
71    /// Returns a shared reference to the main decoded value.
72    #[inline(always)]
73    #[must_use]
74    pub const fn value(&self) -> &V {
75        &self.value
76    }
77
78    /// Returns values emitted while finishing decode state.
79    ///
80    /// # Returns
81    ///
82    /// Returns the finish output in emission order.
83    #[inline(always)]
84    #[must_use]
85    pub fn finish(&self) -> &[V] {
86        &self.finish
87    }
88
89    /// Consumes this result and returns all lifecycle phases.
90    ///
91    /// # Returns
92    ///
93    /// Returns reset values, the main decoded value, and finish values.
94    #[inline(always)]
95    #[must_use]
96    pub fn into_parts(self) -> (Vec<V>, V, Vec<V>) {
97        (self.reset, self.value, self.finish)
98    }
99}