diff options
Diffstat (limited to 'src/codec')
| -rw-r--r-- | src/codec/decode.rs | 43 | ||||
| -rw-r--r-- | src/codec/encode.rs | 43 |
2 files changed, 38 insertions, 48 deletions
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<Self> + impl<Ctx> Decode<Ctx> for $type { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self> 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<Self> +pub trait Decode<Ctx> { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self> where Self: Sized; } -impl Decode for bool { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result<Self> +impl<Ctx> Decode<Ctx> for bool { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self> where Self: Sized, { @@ -35,8 +35,8 @@ impl Decode for bool { } } -impl<T: Decode> Decode for Option<T> { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result<Self> +impl<Ctx, T: Decode<Ctx>> Decode<Ctx> for Option<T> { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self> where Self: Sized, { @@ -49,8 +49,8 @@ impl<T: Decode> Decode for Option<T> { } } -impl<T: Decode> Decode for Vec<T> { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result<Self> +impl<Ctx, T: Decode<Ctx>> Decode<Ctx> for Vec<T> { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self> where Self: Sized, { @@ -63,28 +63,27 @@ impl<T: Decode> Decode for Vec<T> { } } -impl Decode for String { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result<Self> +impl<Ctx> Decode<Ctx> for String { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self> where Self: Sized, { - // assume reader is limited let mut bytes: Vec<u8> = 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<Self> { +impl<Ctx> Decode<Ctx> for Arc<[u8]> { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self> { let mut buf = Vec::with_capacity(DEFAULT_BUFFER_LEN); reader.read_to_end(&mut buf)?; Ok(Arc::<[u8]>::from(buf.into_boxed_slice())) } } -impl<const S: usize> Decode for [u8; S] { - fn decode(reader: &mut dyn Read, _: &dyn Context) -> crate::codec::error::Result<Self> +impl<Ctx, const S: usize> Decode<Ctx> for [u8; S] { + fn decode(reader: &mut dyn Read, _: &mut Ctx) -> Result<Self> 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<Self> +impl<Ctx> Decode<Ctx> for f64 { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self> 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<Self> +impl<Ctx> Decode<Ctx> for f32 { + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> Result<Self> 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<usize, Error> { + impl<Ctx> Encode<Ctx> for $t { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> { 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<usize, Error>; +pub trait Encode<Ctx> { + fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error>; } -impl Encode for bool { - fn encode(&self, buffer: &mut dyn Write, _: &dyn Context) -> Result<usize, Error> { +impl<Ctx> Encode<Ctx> for bool { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> { let size = buffer.write(&[*self as u8])?; Ok(size) } } -impl<T: Encode> Encode for Option<T> { - fn encode(&self, buffer: &mut dyn Write, ctx: &dyn Context) -> Result<usize, Error> { +impl<Ctx, T: Encode<Ctx>> Encode<Ctx> for Option<T> { + fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error> { match self { Some(val) => { let mut size = 1u8.encode(buffer, ctx)?; @@ -36,8 +36,8 @@ impl<T: Encode> Encode for Option<T> { } } -impl<T: Encode> Encode for Vec<T> { - fn encode(&self, buffer: &mut dyn Write, ctx: &dyn Context) -> Result<usize, Error> { +impl<Ctx, T: Encode<Ctx>> Encode<Ctx> for Vec<T> { + fn encode(&self, buffer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, Error> { let mut size = self.len().encode(buffer, ctx)?; for item in self { size += item.encode(buffer, ctx)?; @@ -46,23 +46,23 @@ impl<T: Encode> Encode for Vec<T> { } } -impl Encode for String { - fn encode(&self, buffer: &mut dyn Write, _: &dyn Context) -> Result<usize, Error> { +impl<Ctx> Encode<Ctx> for String { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> { 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<usize, Error> { +impl<Ctx> Encode<Ctx> for Arc<[u8]> { + fn encode(&self, buffer: &mut dyn Write, _: &mut Ctx) -> Result<usize, Error> { let len = buffer.write(self)?; Ok(len) } } -impl<const S: usize> Encode for [u8; S] { - fn encode(&self, buffer: &mut dyn Write, _ctx: &dyn Context) -> Result<usize, Error> { +impl<Ctx, const S: usize> Encode<Ctx> for [u8; S] { + fn encode(&self, buffer: &mut dyn Write, _ctx: &mut Ctx) -> Result<usize, Error> { let len = buffer.write(self)?; Ok(len) } @@ -84,12 +84,3 @@ impl_encode!(isize); impl_encode!(f32); impl_encode!(f64); - -impl<T> Encode for T -where - T: AsRef<dyn Encode>, -{ - fn encode(&self, buffer: &mut dyn Write, ctx: &dyn Context) -> Result<usize, Error> { - self.as_ref().encode(buffer, ctx) - } -} |
