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
// SPDX-License-Identifier: MIT

use crate::{
    traits::{Emitable, Parseable},
    DecodeError,
};

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Config {
    pub key_len: u16,
    pub entry_size: u16,
    pub entries: u32,
    pub last_flush: u32,
    pub last_rand: u32,
    pub hash_rand: u32,
    pub hash_mask: u32,
    pub hash_chain_gc: u32,
    pub proxy_qlen: u32,
}

pub const CONFIG_LEN: usize = 32;

buffer!(ConfigBuffer(CONFIG_LEN) {
    key_len: (u16, 0..2),
    entry_size: (u16, 2..4),
    entries: (u32, 4..8),
    last_flush: (u32, 8..12),
    last_rand: (u32, 12..16),
    hash_rand: (u32, 16..20),
    hash_mask: (u32, 20..24),
    hash_chain_gc: (u32, 24..28),
    proxy_qlen: (u32, 28..32),
});

impl<T: AsRef<[u8]>> Parseable<ConfigBuffer<T>> for Config {
    fn parse(buf: &ConfigBuffer<T>) -> Result<Self, DecodeError> {
        Ok(Self {
            key_len: buf.key_len(),
            entry_size: buf.entry_size(),
            entries: buf.entries(),
            last_flush: buf.last_flush(),
            last_rand: buf.last_rand(),
            hash_rand: buf.hash_rand(),
            hash_mask: buf.hash_mask(),
            hash_chain_gc: buf.hash_chain_gc(),
            proxy_qlen: buf.proxy_qlen(),
        })
    }
}

impl Emitable for Config {
    fn buffer_len(&self) -> usize {
        CONFIG_LEN
    }

    fn emit(&self, buffer: &mut [u8]) {
        let mut buffer = ConfigBuffer::new(buffer);
        buffer.set_key_len(self.key_len);
        buffer.set_entry_size(self.entry_size);
        buffer.set_entries(self.entries);
        buffer.set_last_flush(self.last_flush);
        buffer.set_last_rand(self.last_rand);
        buffer.set_hash_rand(self.hash_rand);
        buffer.set_hash_mask(self.hash_mask);
        buffer.set_hash_chain_gc(self.hash_chain_gc);
        buffer.set_proxy_qlen(self.proxy_qlen);
    }
}