1 #[cfg(test)] 2 mod chunk_test; 3 4 pub(crate) mod chunk_abort; 5 pub(crate) mod chunk_cookie_ack; 6 pub(crate) mod chunk_cookie_echo; 7 pub(crate) mod chunk_error; 8 pub(crate) mod chunk_forward_tsn; 9 pub(crate) mod chunk_header; 10 pub(crate) mod chunk_heartbeat; 11 pub(crate) mod chunk_heartbeat_ack; 12 pub(crate) mod chunk_init; 13 pub mod chunk_payload_data; 14 pub(crate) mod chunk_reconfig; 15 pub(crate) mod chunk_selective_ack; 16 pub(crate) mod chunk_shutdown; 17 pub(crate) mod chunk_shutdown_ack; 18 pub(crate) mod chunk_shutdown_complete; 19 pub(crate) mod chunk_type; 20 pub(crate) mod chunk_unknown; 21 22 use crate::error::{Error, Result}; 23 use chunk_header::*; 24 25 use bytes::{Bytes, BytesMut}; 26 use std::marker::Sized; 27 use std::{any::Any, fmt}; 28 29 pub(crate) trait Chunk: fmt::Display + fmt::Debug { 30 fn header(&self) -> ChunkHeader; 31 fn unmarshal(raw: &Bytes) -> Result<Self> 32 where 33 Self: Sized; 34 fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize>; 35 fn check(&self) -> Result<()>; 36 fn value_length(&self) -> usize; 37 fn as_any(&self) -> &(dyn Any + Send + Sync); 38 39 fn marshal(&self) -> Result<Bytes> { 40 let capacity = CHUNK_HEADER_SIZE + self.value_length(); 41 let mut buf = BytesMut::with_capacity(capacity); 42 self.marshal_to(&mut buf)?; 43 Ok(buf.freeze()) 44 } 45 } 46