1use super::*;
77use anyhow::Result;
78use core::fmt::Debug;
79pub trait CodesReaderFactory<E: Endianness> {
83 type CodesReader<'a>
84 where
85 Self: 'a;
86
87 fn new_reader(&self) -> Self::CodesReader<'_>;
89}
90
91pub trait CodesReaderFactoryHelper<E: Endianness>:
97 for<'a> CodesReaderFactory<E, CodesReader<'a>: CodesRead<E, Error = Self::Error>>
98{
99 type Error;
100}
101
102impl<E: Endianness, F, ERR> CodesReaderFactoryHelper<E> for F
103where
104 F: ?Sized + for<'a> CodesReaderFactory<E, CodesReader<'a>: CodesRead<E, Error = ERR>>,
105{
106 type Error = ERR;
107}
108
109type FactoryReadFn<E, CRF> = for<'a> fn(
115 &mut <CRF as CodesReaderFactory<E>>::CodesReader<'a>,
116)
117 -> Result<u64, <CRF as CodesReaderFactoryHelper<E>>::Error>;
118
119#[derive(Debug, Copy, PartialEq, Eq)]
126pub struct FactoryFuncCodeReader<E: Endianness, CRF: CodesReaderFactoryHelper<E> + ?Sized>(
127 FactoryReadFn<E, CRF>,
128);
129
130impl<E: Endianness, CRF: CodesReaderFactoryHelper<E> + ?Sized> Clone
132 for FactoryFuncCodeReader<E, CRF>
133{
134 #[inline(always)]
135 fn clone(&self) -> Self {
136 Self(self.0)
137 }
138}
139
140impl<E: Endianness, CRF: CodesReaderFactoryHelper<E> + ?Sized> FactoryFuncCodeReader<E, CRF> {
141 const UNARY: FactoryReadFn<E, CRF> = |reader| reader.read_unary();
143 const GAMMA: FactoryReadFn<E, CRF> = |reader| reader.read_gamma();
144 const DELTA: FactoryReadFn<E, CRF> = |reader| reader.read_delta();
145 const OMEGA: FactoryReadFn<E, CRF> = |reader| reader.read_omega();
146 const VBYTE_BE: FactoryReadFn<E, CRF> = |reader| reader.read_vbyte_be();
147 const VBYTE_LE: FactoryReadFn<E, CRF> = |reader| reader.read_vbyte_le();
148 const ZETA2: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(2);
149 const ZETA3: FactoryReadFn<E, CRF> = |reader| reader.read_zeta3();
150 const ZETA4: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(4);
151 const ZETA5: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(5);
152 const ZETA6: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(6);
153 const ZETA7: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(7);
154 const ZETA8: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(8);
155 const ZETA9: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(9);
156 const ZETA10: FactoryReadFn<E, CRF> = |reader| reader.read_zeta(10);
157 const RICE1: FactoryReadFn<E, CRF> = |reader| reader.read_rice(1);
158 const RICE2: FactoryReadFn<E, CRF> = |reader| reader.read_rice(2);
159 const RICE3: FactoryReadFn<E, CRF> = |reader| reader.read_rice(3);
160 const RICE4: FactoryReadFn<E, CRF> = |reader| reader.read_rice(4);
161 const RICE5: FactoryReadFn<E, CRF> = |reader| reader.read_rice(5);
162 const RICE6: FactoryReadFn<E, CRF> = |reader| reader.read_rice(6);
163 const RICE7: FactoryReadFn<E, CRF> = |reader| reader.read_rice(7);
164 const RICE8: FactoryReadFn<E, CRF> = |reader| reader.read_rice(8);
165 const RICE9: FactoryReadFn<E, CRF> = |reader| reader.read_rice(9);
166 const RICE10: FactoryReadFn<E, CRF> = |reader| reader.read_rice(10);
167 const PI1: FactoryReadFn<E, CRF> = |reader| reader.read_pi(1);
168 const PI2: FactoryReadFn<E, CRF> = |reader| reader.read_pi(2);
169 const PI3: FactoryReadFn<E, CRF> = |reader| reader.read_pi(3);
170 const PI4: FactoryReadFn<E, CRF> = |reader| reader.read_pi(4);
171 const PI5: FactoryReadFn<E, CRF> = |reader| reader.read_pi(5);
172 const PI6: FactoryReadFn<E, CRF> = |reader| reader.read_pi(6);
173 const PI7: FactoryReadFn<E, CRF> = |reader| reader.read_pi(7);
174 const PI8: FactoryReadFn<E, CRF> = |reader| reader.read_pi(8);
175 const PI9: FactoryReadFn<E, CRF> = |reader| reader.read_pi(9);
176 const PI10: FactoryReadFn<E, CRF> = |reader| reader.read_pi(10);
177 const GOLOMB3: FactoryReadFn<E, CRF> = |reader| reader.read_golomb(3);
178 const GOLOMB5: FactoryReadFn<E, CRF> = |reader| reader.read_golomb(5);
179 const GOLOMB6: FactoryReadFn<E, CRF> = |reader| reader.read_golomb(6);
180 const GOLOMB7: FactoryReadFn<E, CRF> = |reader| reader.read_golomb(7);
181 const GOLOMB9: FactoryReadFn<E, CRF> = |reader| reader.read_golomb(9);
182 const GOLOMB10: FactoryReadFn<E, CRF> = |reader| reader.read_golomb(10);
183 const EXP_GOLOMB1: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(1);
184 const EXP_GOLOMB2: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(2);
185 const EXP_GOLOMB3: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(3);
186 const EXP_GOLOMB4: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(4);
187 const EXP_GOLOMB5: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(5);
188 const EXP_GOLOMB6: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(6);
189 const EXP_GOLOMB7: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(7);
190 const EXP_GOLOMB8: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(8);
191 const EXP_GOLOMB9: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(9);
192 const EXP_GOLOMB10: FactoryReadFn<E, CRF> = |reader| reader.read_exp_golomb(10);
193
194 pub fn new(code: Codes) -> anyhow::Result<Self> {
201 let read_func = match code {
202 Codes::Unary => Self::UNARY,
203 Codes::Gamma => Self::GAMMA,
204 Codes::Delta => Self::DELTA,
205 Codes::Omega => Self::OMEGA,
206 Codes::VByteBe => Self::VBYTE_BE,
207 Codes::VByteLe => Self::VBYTE_LE,
208 Codes::Zeta { k: 1 } => Self::GAMMA,
209 Codes::Zeta { k: 2 } => Self::ZETA2,
210 Codes::Zeta { k: 3 } => Self::ZETA3,
211 Codes::Zeta { k: 4 } => Self::ZETA4,
212 Codes::Zeta { k: 5 } => Self::ZETA5,
213 Codes::Zeta { k: 6 } => Self::ZETA6,
214 Codes::Zeta { k: 7 } => Self::ZETA7,
215 Codes::Zeta { k: 8 } => Self::ZETA8,
216 Codes::Zeta { k: 9 } => Self::ZETA9,
217 Codes::Zeta { k: 10 } => Self::ZETA10,
218 Codes::Rice { log2_b: 0 } => Self::UNARY,
219 Codes::Rice { log2_b: 1 } => Self::RICE1,
220 Codes::Rice { log2_b: 2 } => Self::RICE2,
221 Codes::Rice { log2_b: 3 } => Self::RICE3,
222 Codes::Rice { log2_b: 4 } => Self::RICE4,
223 Codes::Rice { log2_b: 5 } => Self::RICE5,
224 Codes::Rice { log2_b: 6 } => Self::RICE6,
225 Codes::Rice { log2_b: 7 } => Self::RICE7,
226 Codes::Rice { log2_b: 8 } => Self::RICE8,
227 Codes::Rice { log2_b: 9 } => Self::RICE9,
228 Codes::Rice { log2_b: 10 } => Self::RICE10,
229 Codes::Pi { k: 0 } => Self::GAMMA,
230 Codes::Pi { k: 1 } => Self::PI1,
231 Codes::Pi { k: 2 } => Self::PI2,
232 Codes::Pi { k: 3 } => Self::PI3,
233 Codes::Pi { k: 4 } => Self::PI4,
234 Codes::Pi { k: 5 } => Self::PI5,
235 Codes::Pi { k: 6 } => Self::PI6,
236 Codes::Pi { k: 7 } => Self::PI7,
237 Codes::Pi { k: 8 } => Self::PI8,
238 Codes::Pi { k: 9 } => Self::PI9,
239 Codes::Pi { k: 10 } => Self::PI10,
240 Codes::Golomb { b: 1 } => Self::UNARY,
241 Codes::Golomb { b: 2 } => Self::RICE1,
242 Codes::Golomb { b: 3 } => Self::GOLOMB3,
243 Codes::Golomb { b: 4 } => Self::RICE2,
244 Codes::Golomb { b: 5 } => Self::GOLOMB5,
245 Codes::Golomb { b: 6 } => Self::GOLOMB6,
246 Codes::Golomb { b: 7 } => Self::GOLOMB7,
247 Codes::Golomb { b: 8 } => Self::RICE3,
248 Codes::Golomb { b: 9 } => Self::GOLOMB9,
249 Codes::Golomb { b: 10 } => Self::GOLOMB10,
250 Codes::ExpGolomb { k: 0 } => Self::GAMMA,
251 Codes::ExpGolomb { k: 1 } => Self::EXP_GOLOMB1,
252 Codes::ExpGolomb { k: 2 } => Self::EXP_GOLOMB2,
253 Codes::ExpGolomb { k: 3 } => Self::EXP_GOLOMB3,
254 Codes::ExpGolomb { k: 4 } => Self::EXP_GOLOMB4,
255 Codes::ExpGolomb { k: 5 } => Self::EXP_GOLOMB5,
256 Codes::ExpGolomb { k: 6 } => Self::EXP_GOLOMB6,
257 Codes::ExpGolomb { k: 7 } => Self::EXP_GOLOMB7,
258 Codes::ExpGolomb { k: 8 } => Self::EXP_GOLOMB8,
259 Codes::ExpGolomb { k: 9 } => Self::EXP_GOLOMB9,
260 Codes::ExpGolomb { k: 10 } => Self::EXP_GOLOMB10,
261 _ => anyhow::bail!("Unsupported read dispatch for code {:?}", code),
262 };
263 Ok(Self(read_func))
264 }
265
266 #[inline(always)]
268 pub fn new_with_func(read_func: FactoryReadFn<E, CRF>) -> Self {
269 Self(read_func)
270 }
271
272 #[inline(always)]
274 pub fn inner(&self) -> FactoryReadFn<E, CRF> {
275 self.0
276 }
277
278 #[inline(always)]
281 pub fn get<'a>(
282 &self,
283 ) -> super::FuncCodeReader<E, <CRF as CodesReaderFactory<E>>::CodesReader<'a>> {
284 super::FuncCodeReader::new_with_func(self.0)
285 }
286}