From 7297bbb864778814ed59e99d0d6b05853704f06d Mon Sep 17 00:00:00 2001 From: zirkonya Date: Sun, 16 Aug 2026 15:01:43 +0200 Subject: change context representation --- src/codec/decode.rs | 43 +++++++++++++++++++++---------------------- src/codec/encode.rs | 43 +++++++++++++++++-------------------------- 2 files changed, 38 insertions(+), 48 deletions(-) (limited to 'src/codec') diff --git a/src/codec/decode.rs b/src/codec/decode.rs index 3e03a84..31ed725 100644 --- a/src/codec/decode.rs +++ b/src/codec/decode.rs @@ -1,11 +1,11 @@ use std::{io::Read, sync::Arc}; -use crate::{DEFAULT_BUFFER_LEN, context::Context}; +use crate::{DEFAULT_BUFFER_LEN, codec::error::Result}; macro_rules! impl_decode { ($type: ty) => { - impl Decode for $type { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result + impl Decode for $type { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result where Self: Sized, { @@ -18,14 +18,14 @@ macro_rules! impl_decode { }; } -pub trait Decode { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result +pub trait Decode { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result where Self: Sized; } -impl Decode for bool { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result +impl Decode for bool { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result where Self: Sized, { @@ -35,8 +35,8 @@ impl Decode for bool { } } -impl Decode for Option { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result +impl> Decode for Option { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result where Self: Sized, { @@ -49,8 +49,8 @@ impl Decode for Option { } } -impl Decode for Vec { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result +impl> Decode for Vec { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result where Self: Sized, { @@ -63,28 +63,27 @@ impl Decode for Vec { } } -impl Decode for String { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result +impl Decode for String { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result where Self: Sized, { - // assume reader is limited let mut bytes: Vec = Vec::new(); reader.read_to_end(&mut bytes)?; Ok(String::from_utf8_lossy(&bytes).to_string()) } } -impl Decode for Arc<[u8]> { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result { +impl Decode for Arc<[u8]> { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result { let mut buf = Vec::with_capacity(DEFAULT_BUFFER_LEN); reader.read_to_end(&mut buf)?; Ok(Arc::<[u8]>::from(buf.into_boxed_slice())) } } -impl Decode for [u8; S] { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result +impl Decode for [u8; S] { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result where Self: Sized, { @@ -108,8 +107,8 @@ impl_decode!(i64); impl_decode!(i128); impl_decode!(isize); -impl Decode for f64 { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result +impl Decode for f64 { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result where Self: Sized, { @@ -119,8 +118,8 @@ impl Decode for f64 { } } -impl Decode for f32 { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result +impl Decode for f32 { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result where Self: Sized, { diff --git a/src/codec/encode.rs b/src/codec/encode.rs index 70d9d5c..bd76605 100644 --- a/src/codec/encode.rs +++ b/src/codec/encode.rs @@ -1,10 +1,10 @@ -use crate::{codec::error::Error, context::Context}; +use crate::codec::error::Error; use std::{io::Write, sync::Arc}; macro_rules! impl_encode { ($t: ty) => { - impl Encode for $t { - fn encode(&self, buffer: &mut dyn Write, _: &dyn Context) -> Result { + impl Encode for $t { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result { let size = buffer.write(&self.to_be_bytes())?; Ok(size) } @@ -12,19 +12,19 @@ macro_rules! impl_encode { }; } -pub trait Encode { - fn encode(&self, buffer: &mut dyn Write, ctx: &dyn Context) -> Result; +pub trait Encode { + fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result; } -impl Encode for bool { - fn encode(&self, buffer: &mut dyn Write, _: &dyn Context) -> Result { +impl Encode for bool { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result { let size = buffer.write(&[*self as u8])?; Ok(size) } } -impl Encode for Option { - fn encode(&self, buffer: &mut dyn Write, ctx: &dyn Context) -> Result { +impl> Encode for Option { + fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result { match self { Some(val) => { let mut size = 1u8.encode(buffer, ctx)?; @@ -36,8 +36,8 @@ impl Encode for Option { } } -impl Encode for Vec { - fn encode(&self, buffer: &mut dyn Write, ctx: &dyn Context) -> Result { +impl> Encode for Vec { + fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result { let mut size = self.len().encode(buffer, ctx)?; for item in self { size += item.encode(buffer, ctx)?; @@ -46,23 +46,23 @@ impl Encode for Vec { } } -impl Encode for String { - fn encode(&self, buffer: &mut dyn Write, _: &dyn Context) -> Result { +impl Encode for String { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result { let utf8 = self.as_bytes(); buffer.write_all(utf8)?; Ok(utf8.len()) } } -impl Encode for Arc<[u8]> { - fn encode(&self, buffer: &mut dyn Write, _: &dyn Context) -> Result { +impl Encode for Arc<[u8]> { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result { let len = buffer.write(self)?; Ok(len) } } -impl Encode for [u8; S] { - fn encode(&self, buffer: &mut dyn Write, _ctx: &dyn Context) -> Result { +impl Encode for [u8; S] { + fn encode(&self, buffer: &mut dyn Write, _ctx: &mut Ctx) -> Result { let len = buffer.write(self)?; Ok(len) } @@ -84,12 +84,3 @@ impl_encode!(isize); impl_encode!(f32); impl_encode!(f64); - -impl Encode for T -where - T: AsRef, -{ - fn encode(&self, buffer: &mut dyn Write, ctx: &dyn Context) -> Result { - self.as_ref().encode(buffer, ctx) - } -} -- cgit v1.2.3