1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Policy hooks used by buffered encoder engines.
use EncodeContext;
use crate::;
/// Policy hooks for [`crate::TranscodeEncodeEngine`].
///
/// Hooks own policy state, such as replacement or ignore behavior, but not the
/// codec or engine cursor state. The engine passes the codec into hook methods
/// when policy code needs codec metadata or one-value encode operations.
///
/// Implement this trait when a buffered encoder needs policy decisions around
/// individual values while reusing the common engine loop. Examples include
/// rejecting unsupported values with adapter-level context, consuming values
/// without writing output, writing replacement units, resetting hook-owned
/// state in [`reset_hooks`](Self::reset_hooks), or emitting final state in
/// [`finish_hooks`](Self::finish_hooks).
///
/// The engine calls [`encode_value`](Self::encode_value) for the current input
/// value. The hook either consumes that value and reports written output units,
/// or returns [`EncodeOutcome::NeedOutput`] without consuming it. This lets
/// the engine stop with [`crate::TranscodeStatus::NeedOutput`] and resume later
/// at the same input value.
///
/// # Example
///
/// This hook writes each value with the wrapped codec and reports
/// `NeedOutput` when the current output slice cannot fit the value.
///
/// ```rust
/// use core::{
/// convert::Infallible,
/// num::NonZeroUsize,
/// };
/// use qubit_codec::{
/// TranscodeEncodeHooks,
/// Codec,
/// DecodeFailure,
/// CodecEncodeError,
/// EncodeContext,
/// EncodeOutcome,
/// };
///
/// #[derive(Clone, Copy)]
/// struct ByteCodec;
///
/// impl Codec for ByteCodec {
/// type Value = u8;
/// type Unit = u8;
/// type DecodeError = Infallible;
/// type EncodeError = Infallible;
///
/// const MIN_UNITS_PER_VALUE: NonZeroUsize = NonZeroUsize::MIN;
/// const MAX_UNITS_PER_VALUE: NonZeroUsize = NonZeroUsize::MIN;
///
/// unsafe fn decode(
/// &mut self,
/// input: &[u8],
/// index: usize,
/// ) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
/// Ok((input[index], NonZeroUsize::MIN))
/// }
///
/// unsafe fn encode(
/// &mut self,
/// value: &u8,
/// output: &mut [u8],
/// index: usize,
/// ) -> Result<NonZeroUsize, Self::EncodeError> {
/// output[index] = *value;
/// Ok(NonZeroUsize::MIN)
/// }
/// }
///
/// struct StrictHooks;
///
/// impl<C> TranscodeEncodeHooks<C> for StrictHooks
/// where
/// C: Codec,
/// {
/// type Error = CodecEncodeError<C::EncodeError>;
///
/// fn encode_value(
/// &mut self,
/// codec: &mut C,
/// context: EncodeContext<'_, C::Value, C::Unit>,
/// ) -> Result<EncodeOutcome, Self::Error> {
/// let required = C::MAX_UNITS_PER_VALUE;
/// if context.available_output() < required.get() {
/// return Ok(EncodeOutcome::need_output(required));
/// }
/// let (value, input_index, output, output_index) = context.into_parts();
/// let written = unsafe { codec.encode(value, output, output_index) }
/// .map(NonZeroUsize::get)
/// .map_err(|error| CodecEncodeError::encode(error, input_index))?;
/// Ok(EncodeOutcome::consumed(written))
/// }
/// }
/// ```
///
/// # Type Parameters
///
/// - `C`: Low-level codec owned by the engine.