1use bytes::{Bytes, BytesMut};
2
3pub trait Packet: Sized {
5 type Header;
6
7 fn from_buf(buf: &[u8]) -> Option<Self>;
9
10 fn from_bytes(bytes: Bytes) -> Option<Self>;
12
13 fn to_bytes(&self) -> Bytes;
15
16 fn header(&self) -> Bytes;
18
19 fn payload(&self) -> Bytes;
21
22 fn header_len(&self) -> usize;
24
25 fn payload_len(&self) -> usize;
27 fn total_len(&self) -> usize;
29 fn to_bytes_mut(&self) -> BytesMut {
31 let mut buf = BytesMut::with_capacity(self.total_len());
32 buf.extend_from_slice(&self.to_bytes());
33 buf
34 }
35 fn header_mut(&self) -> BytesMut {
37 let mut buf = BytesMut::with_capacity(self.header_len());
38 buf.extend_from_slice(&self.header());
39 buf
40 }
41 fn payload_mut(&self) -> BytesMut {
43 let mut buf = BytesMut::with_capacity(self.payload_len());
44 buf.extend_from_slice(&self.payload());
45 buf
46 }
47
48 fn into_parts(self) -> (Self::Header, Bytes);
49}