summaryrefslogtreecommitdiff
path: root/src/codec/encode.rs
diff options
context:
space:
mode:
authorzirkonya <zirkonya@iridium.lan>2026-08-25 18:41:28 +0200
committerzirkonya <zirkonya@iridium.lan>2026-08-25 18:41:28 +0200
commit8bae725a803ee00bf5bc86b9fc0be6853b95115e (patch)
tree1a62d1f61191fb3993a1e3d81f34653fe7a10991 /src/codec/encode.rs
parent7297bbb864778814ed59e99d0d6b05853704f06d (diff)
Add getset ; prepare event and backend
Diffstat (limited to 'src/codec/encode.rs')
-rw-r--r--src/codec/encode.rs21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/codec/encode.rs b/src/codec/encode.rs
index bd76605..c8d1c7b 100644
--- a/src/codec/encode.rs
+++ b/src/codec/encode.rs
@@ -1,9 +1,11 @@
+//! Encoding: writing protocol values into a byte stream.
use crate::codec::error::Error;
use std::{io::Write, sync::Arc};
macro_rules! impl_encode {
($t: ty) => {
impl<Ctx> Encode<Ctx> for $t {
+ /// write number using big endian
fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> {
let size = buffer.write(&self.to_be_bytes())?;
Ok(size)
@@ -12,6 +14,7 @@ macro_rules! impl_encode {
};
}
+/// Write a value into a byte stream
pub trait Encode<Ctx> {
fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error>;
}
@@ -24,21 +27,24 @@ impl<Ctx> Encode<Ctx> for bool {
}
impl<Ctx, T: Encode<Ctx>> Encode<Ctx> for Option<T> {
+ /// Encode `T` if Some(T) or do nothing if None
fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error> {
match self {
Some(val) => {
- let mut size = 1u8.encode(buffer, ctx)?;
- size += val.encode(buffer, ctx)?;
+ let size = val.encode(buffer, ctx)?;
Ok(size)
}
- None => 0u8.encode(buffer, ctx),
+ None => Ok(0),
}
}
}
impl<Ctx, T: Encode<Ctx>> Encode<Ctx> for Vec<T> {
+ /// Encode each element of vector
+ /// use `CountPrefix` to prefix the vector with number of elements
+ /// use `LenPrefix` to prefix the vector with encoded byte size
fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error> {
- let mut size = self.len().encode(buffer, ctx)?;
+ let mut size = 0;
for item in self {
size += item.encode(buffer, ctx)?;
}
@@ -47,6 +53,9 @@ impl<Ctx, T: Encode<Ctx>> Encode<Ctx> for Vec<T> {
}
impl<Ctx> Encode<Ctx> for String {
+ /// Encode the string using utf8
+ /// use `CountPrefix` to prefix the string with char length
+ /// use `LenPrefix` to prefix the string with utf8 bytes length
fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> {
let utf8 = self.as_bytes();
buffer.write_all(utf8)?;
@@ -55,6 +64,8 @@ impl<Ctx> Encode<Ctx> for String {
}
impl<Ctx> Encode<Ctx> for Arc<[u8]> {
+ /// write raw slice into the buffer
+ /// use `LenPrefix` to prefix with byte length
fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> {
let len = buffer.write(self)?;
Ok(len)
@@ -62,6 +73,8 @@ impl<Ctx> Encode<Ctx> for Arc<[u8]> {
}
impl<Ctx, const S: usize> Encode<Ctx> for [u8; S] {
+ /// write raw slice into the buffer
+ /// use `LenPrefix` to prefix with byte length
fn encode(&self, buffer: &mut dyn Write, _ctx: &mut Ctx) -> Result<usize, Error> {
let len = buffer.write(self)?;
Ok(len)