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
use crate::{ByteCount, Decode, Encode, Eos, Result, SizedEncode};
#[derive(Debug, Default)]
pub struct NullDecoder;
impl Decode for NullDecoder {
type Item = ();
fn decode(&mut self, _buf: &[u8], _eos: Eos) -> Result<usize> {
Ok(0)
}
fn finish_decoding(&mut self) -> Result<Self::Item> {
Ok(())
}
fn is_idle(&self) -> bool {
true
}
fn requiring_bytes(&self) -> ByteCount {
ByteCount::Finite(0)
}
}
#[derive(Debug, Default)]
pub struct NullEncoder;
impl Encode for NullEncoder {
type Item = ();
fn encode(&mut self, _buf: &mut [u8], _eos: Eos) -> Result<usize> {
Ok(0)
}
fn start_encoding(&mut self, _item: Self::Item) -> Result<()> {
Ok(())
}
fn requiring_bytes(&self) -> ByteCount {
ByteCount::Finite(0)
}
fn is_idle(&self) -> bool {
true
}
}
impl SizedEncode for NullEncoder {
fn exact_requiring_bytes(&self) -> u64 {
0
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn null_decoder_works() {
let mut decoder = NullDecoder;
assert_eq!(decoder.decode(&[1][..], Eos::new(true)).ok(), Some(0));
assert_eq!(decoder.finish_decoding().ok(), Some(()));
assert_eq!(decoder.finish_decoding().ok(), Some(()));
}
#[test]
fn null_encoder_works() {
let mut encoder = NullEncoder;
encoder.start_encoding(()).unwrap();
assert_eq!(encoder.is_idle(), true);
let mut buf = [0; 10];
assert_eq!(encoder.encode(&mut buf[..], Eos::new(true)).ok(), Some(0));
assert_eq!(encoder.is_idle(), true);
}
}