dsi-bitstream 0.9.2

A Rust implementation of read/write bit streams supporting several types of instantaneous codes
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
 * SPDX-FileCopyrightText: 2025 Tommaso Fontana
 * SPDX-FileCopyrightText: 2025 Inria
 * SPDX-FileCopyrightText: 2025 Sebastiano Vigna
 *
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
 */

//! Dynamic dispatching for codes based on function pointers.
//!
//! This kind of dispatch is resolved at runtime, but just once, at construction
//! time, against a specific [`CodesRead`]. The code is stored in a function
//! pointer, so it cannot be inlined like in the [static
//! case](crate::dispatch::static), but the approach is more flexible.

use super::*;
#[cfg(feature = "mem_dbg")]
use mem_dbg::{MemDbg, MemSize};

type ReadFn<E, CR> = fn(&mut CR) -> Result<u64, <CR as BitRead<E>>::Error>;

/// A newtype containing a function pointer dispatching the read
/// method for a code.
///
/// This is a more efficient way to pass a [`StaticCodeRead`] to a method, as a
/// [`FuncCodeReader`] does not need to do a runtime test to dispatch the
/// correct code.
///
/// Instances can be obtained by calling the [`new`](FuncCodeReader::new) method
/// with a variant of the [`Codes`] enum, or by calling the
/// [`new_with_func`](FuncCodeReader::new_with_func) method with a function
/// pointer.
///
/// Note that since selection of the code happens in the
/// [`new`](FuncCodeReader::new) method, it is more efficient to clone a
/// [`FuncCodeReader`] than to create a new one.
#[derive(Debug, Copy)]
#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
pub struct FuncCodeReader<E: Endianness, CR: CodesRead<E> + ?Sized>(ReadFn<E, CR>);

/// Manually implement [`Clone`] to avoid the [`Clone`] bound on CR and E
impl<E: Endianness, CR: CodesRead<E> + ?Sized> Clone for FuncCodeReader<E, CR> {
    #[inline(always)]
    fn clone(&self) -> Self {
        Self(self.0)
    }
}

impl<E: Endianness, CR: CodesRead<E> + ?Sized> FuncCodeReader<E, CR> {
    const UNARY: ReadFn<E, CR> = |reader: &mut CR| reader.read_unary();
    const GAMMA: ReadFn<E, CR> = |reader: &mut CR| reader.read_gamma();
    const DELTA: ReadFn<E, CR> = |reader: &mut CR| reader.read_delta();
    const OMEGA: ReadFn<E, CR> = |reader: &mut CR| reader.read_omega();
    const VBYTE_BE: ReadFn<E, CR> = |reader: &mut CR| reader.read_vbyte_be();
    const VBYTE_LE: ReadFn<E, CR> = |reader: &mut CR| reader.read_vbyte_le();
    const ZETA2: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(2);
    const ZETA3: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta3();
    const ZETA4: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(4);
    const ZETA5: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(5);
    const ZETA6: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(6);
    const ZETA7: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(7);
    const ZETA8: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(8);
    const ZETA9: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(9);
    const ZETA10: ReadFn<E, CR> = |reader: &mut CR| reader.read_zeta(10);
    const RICE1: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(1);
    const RICE2: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(2);
    const RICE3: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(3);
    const RICE4: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(4);
    const RICE5: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(5);
    const RICE6: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(6);
    const RICE7: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(7);
    const RICE8: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(8);
    const RICE9: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(9);
    const RICE10: ReadFn<E, CR> = |reader: &mut CR| reader.read_rice(10);
    const PI1: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(1);
    const PI2: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi2();
    const PI3: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(3);
    const PI4: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(4);
    const PI5: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(5);
    const PI6: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(6);
    const PI7: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(7);
    const PI8: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(8);
    const PI9: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(9);
    const PI10: ReadFn<E, CR> = |reader: &mut CR| reader.read_pi(10);
    const GOLOMB3: ReadFn<E, CR> = |reader: &mut CR| reader.read_golomb(3);
    const GOLOMB5: ReadFn<E, CR> = |reader: &mut CR| reader.read_golomb(5);
    const GOLOMB6: ReadFn<E, CR> = |reader: &mut CR| reader.read_golomb(6);
    const GOLOMB7: ReadFn<E, CR> = |reader: &mut CR| reader.read_golomb(7);
    const GOLOMB9: ReadFn<E, CR> = |reader: &mut CR| reader.read_golomb(9);
    const GOLOMB10: ReadFn<E, CR> = |reader: &mut CR| reader.read_golomb(10);
    const EXP_GOLOMB1: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(1);
    const EXP_GOLOMB2: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(2);
    const EXP_GOLOMB3: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(3);
    const EXP_GOLOMB4: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(4);
    const EXP_GOLOMB5: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(5);
    const EXP_GOLOMB6: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(6);
    const EXP_GOLOMB7: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(7);
    const EXP_GOLOMB8: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(8);
    const EXP_GOLOMB9: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(9);
    const EXP_GOLOMB10: ReadFn<E, CR> = |reader: &mut CR| reader.read_exp_golomb(10);

    /// Returns a new [`FuncCodeReader`] for the given code.
    ///
    /// The code is [canonicalized](Codes::canonicalize) before
    /// the lookup, so equivalent codes yield the same reader.
    ///
    /// # Errors
    ///
    /// The method will return an error if there is no constant
    /// for the given code in [`FuncCodeReader`].
    pub const fn new(code: Codes) -> Result<Self, DispatchError> {
        let code = code.canonicalize();
        let read_func = match code {
            Codes::Unary => Self::UNARY,
            Codes::Gamma => Self::GAMMA,
            Codes::Delta => Self::DELTA,
            Codes::Omega => Self::OMEGA,
            Codes::VByteBe => Self::VBYTE_BE,
            Codes::VByteLe => Self::VBYTE_LE,
            Codes::Zeta(2) => Self::ZETA2,
            Codes::Zeta(3) => Self::ZETA3,
            Codes::Zeta(4) => Self::ZETA4,
            Codes::Zeta(5) => Self::ZETA5,
            Codes::Zeta(6) => Self::ZETA6,
            Codes::Zeta(7) => Self::ZETA7,
            Codes::Zeta(8) => Self::ZETA8,
            Codes::Zeta(9) => Self::ZETA9,
            Codes::Zeta(10) => Self::ZETA10,
            Codes::Rice(1) => Self::RICE1,
            Codes::Rice(2) => Self::RICE2,
            Codes::Rice(3) => Self::RICE3,
            Codes::Rice(4) => Self::RICE4,
            Codes::Rice(5) => Self::RICE5,
            Codes::Rice(6) => Self::RICE6,
            Codes::Rice(7) => Self::RICE7,
            Codes::Rice(8) => Self::RICE8,
            Codes::Rice(9) => Self::RICE9,
            Codes::Rice(10) => Self::RICE10,
            Codes::Pi(1) => Self::PI1,
            Codes::Pi(2) => Self::PI2,
            Codes::Pi(3) => Self::PI3,
            Codes::Pi(4) => Self::PI4,
            Codes::Pi(5) => Self::PI5,
            Codes::Pi(6) => Self::PI6,
            Codes::Pi(7) => Self::PI7,
            Codes::Pi(8) => Self::PI8,
            Codes::Pi(9) => Self::PI9,
            Codes::Pi(10) => Self::PI10,
            Codes::Golomb(3) => Self::GOLOMB3,
            Codes::Golomb(5) => Self::GOLOMB5,
            Codes::Golomb(6) => Self::GOLOMB6,
            Codes::Golomb(7) => Self::GOLOMB7,
            Codes::Golomb(9) => Self::GOLOMB9,
            Codes::Golomb(10) => Self::GOLOMB10,
            Codes::ExpGolomb(1) => Self::EXP_GOLOMB1,
            Codes::ExpGolomb(2) => Self::EXP_GOLOMB2,
            Codes::ExpGolomb(3) => Self::EXP_GOLOMB3,
            Codes::ExpGolomb(4) => Self::EXP_GOLOMB4,
            Codes::ExpGolomb(5) => Self::EXP_GOLOMB5,
            Codes::ExpGolomb(6) => Self::EXP_GOLOMB6,
            Codes::ExpGolomb(7) => Self::EXP_GOLOMB7,
            Codes::ExpGolomb(8) => Self::EXP_GOLOMB8,
            Codes::ExpGolomb(9) => Self::EXP_GOLOMB9,
            Codes::ExpGolomb(10) => Self::EXP_GOLOMB10,
            _ => return Err(DispatchError::UnsupportedCode(code)),
        };
        Ok(Self(read_func))
    }

    /// Returns a new [`FuncCodeReader`] for the given function.
    #[must_use]
    #[inline(always)]
    pub const fn new_with_func(read_func: ReadFn<E, CR>) -> Self {
        Self(read_func)
    }

    /// Returns the function pointer for the code.
    #[must_use]
    #[inline(always)]
    pub const fn get_func(&self) -> ReadFn<E, CR> {
        self.0
    }
}

impl<E: Endianness, CR: CodesRead<E> + ?Sized> StaticCodeRead<E, CR> for FuncCodeReader<E, CR> {
    #[inline(always)]
    fn read(&self, reader: &mut CR) -> Result<u64, CR::Error> {
        (self.0)(reader)
    }
}

type WriteFn<E, CW> = fn(&mut CW, u64) -> Result<usize, <CW as BitWrite<E>>::Error>;

/// A newtype containing a function pointer dispatching the write method for a
/// code.
///
/// This is a more efficient way to pass a [`StaticCodeWrite`] to a method, as
/// a [`FuncCodeWriter`] does not need to do a runtime test to dispatch the
/// correct code.
///
/// Instances can be obtained by calling the [`new`](FuncCodeWriter::new) method
/// with a variant of the [`Codes`] enum, or by calling the
/// [`new_with_func`](FuncCodeWriter::new_with_func) method with a function
/// pointer.
///
/// Note that since selection of the code happens in the
/// [`new`](FuncCodeWriter::new) method, it is more efficient to clone a
/// [`FuncCodeWriter`] than to create a new one.
#[derive(Debug, Copy)]
#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
pub struct FuncCodeWriter<E: Endianness, CW: CodesWrite<E> + ?Sized>(WriteFn<E, CW>);

/// Manually implement [`Clone`] to avoid the [`Clone`] bound on CW and E.
impl<E: Endianness, CW: CodesWrite<E> + ?Sized> Clone for FuncCodeWriter<E, CW> {
    #[inline(always)]
    fn clone(&self) -> Self {
        Self(self.0)
    }
}

impl<E: Endianness, CW: CodesWrite<E> + ?Sized> FuncCodeWriter<E, CW> {
    const UNARY: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_unary(n);
    const GAMMA: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_gamma(n);
    const DELTA: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_delta(n);
    const OMEGA: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_omega(n);
    const VBYTE_BE: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_vbyte_be(n);
    const VBYTE_LE: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_vbyte_le(n);
    const ZETA2: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 2);
    const ZETA3: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta3(n);
    const ZETA4: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 4);
    const ZETA5: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 5);
    const ZETA6: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 6);
    const ZETA7: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 7);
    const ZETA8: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 8);
    const ZETA9: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 9);
    const ZETA10: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_zeta(n, 10);
    const RICE1: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 1);
    const RICE2: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 2);
    const RICE3: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 3);
    const RICE4: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 4);
    const RICE5: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 5);
    const RICE6: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 6);
    const RICE7: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 7);
    const RICE8: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 8);
    const RICE9: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 9);
    const RICE10: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_rice(n, 10);
    const PI1: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 1);
    const PI2: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi2(n);
    const PI3: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 3);
    const PI4: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 4);
    const PI5: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 5);
    const PI6: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 6);
    const PI7: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 7);
    const PI8: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 8);
    const PI9: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 9);
    const PI10: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_pi(n, 10);
    const GOLOMB3: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_golomb(n, 3);
    const GOLOMB5: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_golomb(n, 5);
    const GOLOMB6: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_golomb(n, 6);
    const GOLOMB7: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_golomb(n, 7);
    const GOLOMB9: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_golomb(n, 9);
    const GOLOMB10: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_golomb(n, 10);
    const EXP_GOLOMB1: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 1);
    const EXP_GOLOMB2: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 2);
    const EXP_GOLOMB3: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 3);
    const EXP_GOLOMB4: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 4);
    const EXP_GOLOMB5: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 5);
    const EXP_GOLOMB6: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 6);
    const EXP_GOLOMB7: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 7);
    const EXP_GOLOMB8: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 8);
    const EXP_GOLOMB9: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 9);
    const EXP_GOLOMB10: WriteFn<E, CW> = |writer: &mut CW, n: u64| writer.write_exp_golomb(n, 10);

    /// Returns a new [`FuncCodeWriter`] for the given code.
    ///
    /// The code is [canonicalized](Codes::canonicalize) before
    /// the lookup, so equivalent codes yield the same writer.
    ///
    /// # Errors
    ///
    /// The method will return an error if there is no constant
    /// for the given code in [`FuncCodeWriter`].
    pub const fn new(code: Codes) -> Result<Self, DispatchError> {
        let code = code.canonicalize();
        let write_func = match code {
            Codes::Unary => Self::UNARY,
            Codes::Gamma => Self::GAMMA,
            Codes::Delta => Self::DELTA,
            Codes::Omega => Self::OMEGA,
            Codes::VByteBe => Self::VBYTE_BE,
            Codes::VByteLe => Self::VBYTE_LE,
            Codes::Zeta(2) => Self::ZETA2,
            Codes::Zeta(3) => Self::ZETA3,
            Codes::Zeta(4) => Self::ZETA4,
            Codes::Zeta(5) => Self::ZETA5,
            Codes::Zeta(6) => Self::ZETA6,
            Codes::Zeta(7) => Self::ZETA7,
            Codes::Zeta(8) => Self::ZETA8,
            Codes::Zeta(9) => Self::ZETA9,
            Codes::Zeta(10) => Self::ZETA10,
            Codes::Rice(1) => Self::RICE1,
            Codes::Rice(2) => Self::RICE2,
            Codes::Rice(3) => Self::RICE3,
            Codes::Rice(4) => Self::RICE4,
            Codes::Rice(5) => Self::RICE5,
            Codes::Rice(6) => Self::RICE6,
            Codes::Rice(7) => Self::RICE7,
            Codes::Rice(8) => Self::RICE8,
            Codes::Rice(9) => Self::RICE9,
            Codes::Rice(10) => Self::RICE10,
            Codes::Pi(1) => Self::PI1,
            Codes::Pi(2) => Self::PI2,
            Codes::Pi(3) => Self::PI3,
            Codes::Pi(4) => Self::PI4,
            Codes::Pi(5) => Self::PI5,
            Codes::Pi(6) => Self::PI6,
            Codes::Pi(7) => Self::PI7,
            Codes::Pi(8) => Self::PI8,
            Codes::Pi(9) => Self::PI9,
            Codes::Pi(10) => Self::PI10,
            Codes::Golomb(3) => Self::GOLOMB3,
            Codes::Golomb(5) => Self::GOLOMB5,
            Codes::Golomb(6) => Self::GOLOMB6,
            Codes::Golomb(7) => Self::GOLOMB7,
            Codes::Golomb(9) => Self::GOLOMB9,
            Codes::Golomb(10) => Self::GOLOMB10,
            Codes::ExpGolomb(1) => Self::EXP_GOLOMB1,
            Codes::ExpGolomb(2) => Self::EXP_GOLOMB2,
            Codes::ExpGolomb(3) => Self::EXP_GOLOMB3,
            Codes::ExpGolomb(4) => Self::EXP_GOLOMB4,
            Codes::ExpGolomb(5) => Self::EXP_GOLOMB5,
            Codes::ExpGolomb(6) => Self::EXP_GOLOMB6,
            Codes::ExpGolomb(7) => Self::EXP_GOLOMB7,
            Codes::ExpGolomb(8) => Self::EXP_GOLOMB8,
            Codes::ExpGolomb(9) => Self::EXP_GOLOMB9,
            Codes::ExpGolomb(10) => Self::EXP_GOLOMB10,
            _ => return Err(DispatchError::UnsupportedCode(code)),
        };
        Ok(Self(write_func))
    }

    /// Returns a new [`FuncCodeWriter`] for the given function.
    #[must_use]
    #[inline(always)]
    pub const fn new_with_func(write_func: WriteFn<E, CW>) -> Self {
        Self(write_func)
    }

    /// Returns the function pointer for the code.
    #[must_use]
    #[inline(always)]
    pub const fn get_func(&self) -> WriteFn<E, CW> {
        self.0
    }
}

impl<E: Endianness, CW: CodesWrite<E> + ?Sized> StaticCodeWrite<E, CW> for FuncCodeWriter<E, CW> {
    #[inline(always)]
    fn write(&self, writer: &mut CW, n: u64) -> Result<usize, CW::Error> {
        (self.0)(writer, n)
    }
}

type LenFn = fn(u64) -> usize;

/// A newtype containing a function pointer dispatching the length method for a
/// code.
///
/// This is a more efficient way to pass a [`CodeLen`] to a method, as
/// a [`FuncCodeLen`] does not need to do a runtime test to dispatch the correct
/// method.
///
/// Instances can be obtained by calling the [`new`](FuncCodeLen::new) method
/// with a variant of the [`Codes`] enum, or by calling the
/// [`new_with_func`](FuncCodeLen::new_with_func) method with a function pointer.
///
/// Note that since selection of the code happens in the [`new`](FuncCodeLen::new)
/// method, it is more efficient to clone a [`FuncCodeLen`] than to create a new one.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
#[cfg_attr(feature = "mem_dbg", mem_size(flat))]
pub struct FuncCodeLen(LenFn);

impl FuncCodeLen {
    const UNARY: LenFn = |n| n as usize + 1;
    const GAMMA: LenFn = |n| len_gamma(n);
    const DELTA: LenFn = |n| len_delta(n);
    const OMEGA: LenFn = |n| len_omega(n);
    const VBYTE_BE: LenFn = |n| bit_len_vbyte(n);
    const VBYTE_LE: LenFn = |n| bit_len_vbyte(n);
    const ZETA2: LenFn = |n| len_zeta(n, 2);
    const ZETA3: LenFn = |n| len_zeta(n, 3);
    const ZETA4: LenFn = |n| len_zeta(n, 4);
    const ZETA5: LenFn = |n| len_zeta(n, 5);
    const ZETA6: LenFn = |n| len_zeta(n, 6);
    const ZETA7: LenFn = |n| len_zeta(n, 7);
    const ZETA8: LenFn = |n| len_zeta(n, 8);
    const ZETA9: LenFn = |n| len_zeta(n, 9);
    const ZETA10: LenFn = |n| len_zeta(n, 10);
    const RICE1: LenFn = |n| len_rice(n, 1);
    const RICE2: LenFn = |n| len_rice(n, 2);
    const RICE3: LenFn = |n| len_rice(n, 3);
    const RICE4: LenFn = |n| len_rice(n, 4);
    const RICE5: LenFn = |n| len_rice(n, 5);
    const RICE6: LenFn = |n| len_rice(n, 6);
    const RICE7: LenFn = |n| len_rice(n, 7);
    const RICE8: LenFn = |n| len_rice(n, 8);
    const RICE9: LenFn = |n| len_rice(n, 9);
    const RICE10: LenFn = |n| len_rice(n, 10);
    const PI1: LenFn = |n| len_pi(n, 1);
    const PI2: LenFn = |n| len_pi(n, 2);
    const PI3: LenFn = |n| len_pi(n, 3);
    const PI4: LenFn = |n| len_pi(n, 4);
    const PI5: LenFn = |n| len_pi(n, 5);
    const PI6: LenFn = |n| len_pi(n, 6);
    const PI7: LenFn = |n| len_pi(n, 7);
    const PI8: LenFn = |n| len_pi(n, 8);
    const PI9: LenFn = |n| len_pi(n, 9);
    const PI10: LenFn = |n| len_pi(n, 10);
    const GOLOMB3: LenFn = |n| len_golomb(n, 3);
    const GOLOMB5: LenFn = |n| len_golomb(n, 5);
    const GOLOMB6: LenFn = |n| len_golomb(n, 6);
    const GOLOMB7: LenFn = |n| len_golomb(n, 7);
    const GOLOMB9: LenFn = |n| len_golomb(n, 9);
    const GOLOMB10: LenFn = |n| len_golomb(n, 10);
    const EXP_GOLOMB1: LenFn = |n| len_exp_golomb(n, 1);
    const EXP_GOLOMB2: LenFn = |n| len_exp_golomb(n, 2);
    const EXP_GOLOMB3: LenFn = |n| len_exp_golomb(n, 3);
    const EXP_GOLOMB4: LenFn = |n| len_exp_golomb(n, 4);
    const EXP_GOLOMB5: LenFn = |n| len_exp_golomb(n, 5);
    const EXP_GOLOMB6: LenFn = |n| len_exp_golomb(n, 6);
    const EXP_GOLOMB7: LenFn = |n| len_exp_golomb(n, 7);
    const EXP_GOLOMB8: LenFn = |n| len_exp_golomb(n, 8);
    const EXP_GOLOMB9: LenFn = |n| len_exp_golomb(n, 9);
    const EXP_GOLOMB10: LenFn = |n| len_exp_golomb(n, 10);

    /// Returns a new [`FuncCodeLen`] for the given code.
    ///
    /// The code is [canonicalized](Codes::canonicalize) before
    /// the lookup, so equivalent codes yield the same length
    /// function.
    ///
    /// # Errors
    ///
    /// The method will return an error if there is no constant
    /// for the given code in [`FuncCodeLen`].
    pub const fn new(code: Codes) -> Result<Self, DispatchError> {
        let code = code.canonicalize();
        let len_func = match code {
            Codes::Unary => Self::UNARY,
            Codes::Gamma => Self::GAMMA,
            Codes::Delta => Self::DELTA,
            Codes::Omega => Self::OMEGA,
            Codes::VByteBe => Self::VBYTE_BE,
            Codes::VByteLe => Self::VBYTE_LE,
            Codes::Zeta(2) => Self::ZETA2,
            Codes::Zeta(3) => Self::ZETA3,
            Codes::Zeta(4) => Self::ZETA4,
            Codes::Zeta(5) => Self::ZETA5,
            Codes::Zeta(6) => Self::ZETA6,
            Codes::Zeta(7) => Self::ZETA7,
            Codes::Zeta(8) => Self::ZETA8,
            Codes::Zeta(9) => Self::ZETA9,
            Codes::Zeta(10) => Self::ZETA10,
            Codes::Rice(1) => Self::RICE1,
            Codes::Rice(2) => Self::RICE2,
            Codes::Rice(3) => Self::RICE3,
            Codes::Rice(4) => Self::RICE4,
            Codes::Rice(5) => Self::RICE5,
            Codes::Rice(6) => Self::RICE6,
            Codes::Rice(7) => Self::RICE7,
            Codes::Rice(8) => Self::RICE8,
            Codes::Rice(9) => Self::RICE9,
            Codes::Rice(10) => Self::RICE10,
            Codes::Pi(1) => Self::PI1,
            Codes::Pi(2) => Self::PI2,
            Codes::Pi(3) => Self::PI3,
            Codes::Pi(4) => Self::PI4,
            Codes::Pi(5) => Self::PI5,
            Codes::Pi(6) => Self::PI6,
            Codes::Pi(7) => Self::PI7,
            Codes::Pi(8) => Self::PI8,
            Codes::Pi(9) => Self::PI9,
            Codes::Pi(10) => Self::PI10,
            Codes::Golomb(3) => Self::GOLOMB3,
            Codes::Golomb(5) => Self::GOLOMB5,
            Codes::Golomb(6) => Self::GOLOMB6,
            Codes::Golomb(7) => Self::GOLOMB7,
            Codes::Golomb(9) => Self::GOLOMB9,
            Codes::Golomb(10) => Self::GOLOMB10,
            Codes::ExpGolomb(1) => Self::EXP_GOLOMB1,
            Codes::ExpGolomb(2) => Self::EXP_GOLOMB2,
            Codes::ExpGolomb(3) => Self::EXP_GOLOMB3,
            Codes::ExpGolomb(4) => Self::EXP_GOLOMB4,
            Codes::ExpGolomb(5) => Self::EXP_GOLOMB5,
            Codes::ExpGolomb(6) => Self::EXP_GOLOMB6,
            Codes::ExpGolomb(7) => Self::EXP_GOLOMB7,
            Codes::ExpGolomb(8) => Self::EXP_GOLOMB8,
            Codes::ExpGolomb(9) => Self::EXP_GOLOMB9,
            Codes::ExpGolomb(10) => Self::EXP_GOLOMB10,
            _ => return Err(DispatchError::UnsupportedCode(code)),
        };
        Ok(Self(len_func))
    }

    /// Returns a new [`FuncCodeLen`] for the given function.
    #[must_use]
    #[inline(always)]
    pub const fn new_with_func(len_func: LenFn) -> Self {
        Self(len_func)
    }
    /// Returns the function pointer for the code.
    #[must_use]
    #[inline(always)]
    pub const fn get_func(&self) -> LenFn {
        self.0
    }
}

/// Here we do not depend on the bitstream, so there is no need for a "static"
/// version of the trait.
impl CodeLen for FuncCodeLen {
    #[inline(always)]
    fn len(&self, n: u64) -> usize {
        (self.0)(n)
    }
}