INNER CODE UNIT · Rust
new
qaul/qaul.net · rust/libp2p_modules/varint_prefixed_codec/src/lib.rs:32
pub fn new(max_message_len_bytes: usize) -> Self {
Self {
max_message_len_bytes,
}
}
}
impl Encoder for VarintPrefixedCodec {
type Item<'a> = Vec<u8>;
type Error = Error;
/// Encode the `Vec<u8>` raw bytes message, by adding a message length prefix
/// which is encoded as an `unsigned_varint`.
/// The encoder further checks if the raw bytes packages exceeds the configured maximal message length.
fn encode(&mut self, src: Self::Item<'_>, dst: &mut BytesMut) -> Result<(), Self::Error> {
// check length of package to send and return if package limit is exceeded
let message_length = src.len();
if message_length > self.max_message_len_bytes {