qubit_codec/transcode/engine/encode_context.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//! Read-only policy context for one buffered encode attempt.
9
10/// Read-only context supplied to an encode policy hook.
11///
12/// The engine retains ownership of the mutable output slice. Hooks can inspect
13/// the current value and cursor/capacity metadata, then return an
14/// [`crate::engine::EncodeUnencodableAction`] without being able to mutate
15/// output behind the engine's progress accounting.
16///
17/// # Type Parameters
18///
19/// - `Value`: Logical input value type.
20#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
21pub struct EncodeContext<'a, Value> {
22 input_value: &'a Value,
23 input_index: usize,
24 output_index: usize,
25 available_output: usize,
26}
27
28impl<'a, Value> EncodeContext<'a, Value> {
29 /// Creates an encode policy context.
30 ///
31 /// # Parameters
32 ///
33 /// - `input_value`: Borrowed input value being encoded.
34 /// - `input_index`: Absolute input index of `input_value`.
35 /// - `output_index`: Absolute output index where writing begins.
36 /// - `available_output`: Output units writable from `output_index`.
37 ///
38 /// # Returns
39 ///
40 /// Returns a read-only encode context.
41 #[inline(always)]
42 #[must_use]
43 pub const fn new(
44 input_value: &'a Value,
45 input_index: usize,
46 output_index: usize,
47 available_output: usize,
48 ) -> Self {
49 Self {
50 input_value,
51 input_index,
52 output_index,
53 available_output,
54 }
55 }
56
57 /// Returns the input value being encoded.
58 #[inline(always)]
59 #[must_use]
60 pub const fn input_value(&self) -> &Value {
61 self.input_value
62 }
63
64 /// Returns the absolute input index of the current value.
65 #[inline(always)]
66 #[must_use]
67 pub const fn input_index(&self) -> usize {
68 self.input_index
69 }
70
71 /// Returns the absolute output index where writing begins.
72 #[inline(always)]
73 #[must_use]
74 pub const fn output_index(&self) -> usize {
75 self.output_index
76 }
77
78 /// Returns output capacity visible to this encode attempt.
79 #[inline(always)]
80 #[must_use]
81 pub const fn available_output(&self) -> usize {
82 self.available_output
83 }
84}