kinesin_rdt/frame/
mod.rs

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
#![allow(clippy::result_unit_err)] // todo
pub mod buffer_util;
pub mod encoding;
pub mod stream;

pub use stream::*;

// TODO: helpers for serialization, maybe macros?
// TODO: graceful error handling for too-short reads

/// frame serialization
pub trait Serialize {
    /// determine serialized length of frame
    fn serialized_length(&self) -> usize;
    /// write frame to buffer, returning serialized length
    fn write(&self, buf: &mut [u8]) -> usize;
    /// read frame from buffer, returning frame and serialized length
    fn read(buf: &[u8]) -> Result<(usize, Self), ()>
    where
        Self: Sized;

    /// whether the frame has special "serialize to end" behavior
    fn has_end_optimization() -> bool
    where
        Self: Sized,
    {
        false
    }
}

/// frame serialization allowing optimizations for end-of-packet frames
pub trait SerializeToEnd: Serialize {
    /// determine serialized length of frame at the end of the packet
    fn serialized_length_at_end(&self) -> usize {
        self.serialized_length()
    }

    /// write last frame of packet to buffer, returning serialized length
    fn write_to_end(&self, buf: &mut [u8]) -> usize {
        self.write(buf)
    }

    /// read last frame of packet from buffer, returning frame
    fn read_to_end(buf: &[u8]) -> Result<Self, ()>
    where
        Self: Sized,
    {
        Self::read(buf).map(|r| r.1)
    }

    /// whether the frame has special "serialize to end" behavior
    fn has_end_optimization() -> bool
    where
        Self: Sized,
    {
        true
    }
}