kinesin_rdt/frame/mod.rs
1#![allow(clippy::result_unit_err)] // todo
2pub mod buffer_util;
3pub mod encoding;
4pub mod stream;
5
6pub use stream::*;
7
8// TODO: helpers for serialization, maybe macros?
9// TODO: graceful error handling for too-short reads
10
11/// frame serialization
12pub trait Serialize {
13 /// determine serialized length of frame
14 fn serialized_length(&self) -> usize;
15 /// write frame to buffer, returning serialized length
16 fn write(&self, buf: &mut [u8]) -> usize;
17 /// read frame from buffer, returning frame and serialized length
18 fn read(buf: &[u8]) -> Result<(usize, Self), ()>
19 where
20 Self: Sized;
21
22 /// whether the frame has special "serialize to end" behavior
23 fn has_end_optimization() -> bool
24 where
25 Self: Sized,
26 {
27 false
28 }
29}
30
31/// frame serialization allowing optimizations for end-of-packet frames
32pub trait SerializeToEnd: Serialize {
33 /// determine serialized length of frame at the end of the packet
34 fn serialized_length_at_end(&self) -> usize {
35 self.serialized_length()
36 }
37
38 /// write last frame of packet to buffer, returning serialized length
39 fn write_to_end(&self, buf: &mut [u8]) -> usize {
40 self.write(buf)
41 }
42
43 /// read last frame of packet from buffer, returning frame
44 fn read_to_end(buf: &[u8]) -> Result<Self, ()>
45 where
46 Self: Sized,
47 {
48 Self::read(buf).map(|r| r.1)
49 }
50
51 /// whether the frame has special "serialize to end" behavior
52 fn has_end_optimization() -> bool
53 where
54 Self: Sized,
55 {
56 true
57 }
58}