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
// SPDX-License-Identifier: Apache-2.0
/// Cache hit/miss counters reported by codec contexts.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct CacheStats {
/// Number of cache lookups that reused existing state.
pub hits: u64,
/// Number of cache lookups that had to build new state.
pub misses: u64,
/// Number of currently occupied cache slots.
pub occupied_slots: u64,
/// Number of cache entries evicted by later insertions.
pub evictions: u64,
}
impl CacheStats {
/// Construct cache statistics from explicit counters.
pub const fn new(hits: u64, misses: u64) -> Self {
Self {
hits,
misses,
occupied_slots: 0,
evictions: 0,
}
}
/// Construct cache statistics from full counters.
pub const fn with_slots(hits: u64, misses: u64, occupied_slots: u64, evictions: u64) -> Self {
Self {
hits,
misses,
occupied_slots,
evictions,
}
}
}
/// Reusable codec state cached across decode calls.
pub trait CodecContext: Default + Send {
/// Drop cached state while keeping the context reusable.
fn clear(&mut self);
/// Return current cache counters, when the codec tracks them.
fn cache_stats(&self) -> CacheStats {
CacheStats::default()
}
}
/// Wrapper that owns codec context state for repeated decode calls.
#[derive(Debug, Default)]
pub struct DecoderContext<C: CodecContext> {
codec: C,
}
impl<C: CodecContext> DecoderContext<C> {
/// Construct an empty decoder context.
pub fn new() -> Self {
Self {
codec: C::default(),
}
}
/// Borrow the codec-specific context.
pub fn codec(&self) -> &C {
&self.codec
}
/// Mutably borrow the codec-specific context.
pub fn codec_mut(&mut self) -> &mut C {
&mut self.codec
}
/// Clear cached codec state.
pub fn clear(&mut self) {
self.codec.clear();
}
/// Return cache counters from the codec-specific context.
pub fn cache_stats(&self) -> CacheStats {
self.codec.cache_stats()
}
/// Consume the wrapper and return the codec-specific context.
pub fn into_inner(self) -> C {
self.codec
}
}