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

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

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct CacheInfo {
    pub clntref: u32,
    pub last_use: u32,
    pub expires: u32,
    pub error: u32,
    pub used: u32,
    pub id: u32,
    pub ts: u32,
    pub ts_age: u32,
}

pub const CACHE_INFO_LEN: usize = 32;

buffer!(CacheInfoBuffer(CACHE_INFO_LEN) {
    clntref: (u32, 0..4),
    last_use: (u32, 4..8),
    expires: (u32, 8..12),
    error: (u32, 12..16),
    used: (u32, 16..20),
    id: (u32, 20..24),
    ts: (u32, 24..28),
    ts_age: (u32, 28..32),
});

impl<T: AsRef<[u8]>> Parseable<CacheInfoBuffer<T>> for CacheInfo {
    fn parse(buf: &CacheInfoBuffer<T>) -> Result<Self, DecodeError> {
        Ok(Self {
            clntref: buf.clntref(),
            last_use: buf.last_use(),
            expires: buf.expires(),
            error: buf.error(),
            used: buf.used(),
            id: buf.id(),
            ts: buf.ts(),
            ts_age: buf.ts_age(),
        })
    }
}

impl Emitable for CacheInfo {
    fn buffer_len(&self) -> usize {
        CACHE_INFO_LEN
    }

    fn emit(&self, buffer: &mut [u8]) {
        let mut buffer = CacheInfoBuffer::new(buffer);
        buffer.set_clntref(self.clntref);
        buffer.set_last_use(self.last_use);
        buffer.set_expires(self.expires);
        buffer.set_error(self.error);
        buffer.set_used(self.used);
        buffer.set_id(self.id);
        buffer.set_ts(self.ts);
        buffer.set_ts_age(self.ts_age);
    }
}