diff options
| author | zirkonya <zirkonya@iridium.lan> | 2026-08-16 15:01:43 +0200 |
|---|---|---|
| committer | zirkonya <zirkonya@iridium.lan> | 2026-08-16 15:01:43 +0200 |
| commit | 7297bbb864778814ed59e99d0d6b05853704f06d (patch) | |
| tree | b685e39344de04dc9ef6c103d11ec0d50d962cb1 | |
| parent | 7cfd669783c310747debcfca7106e6162f492340 (diff) | |
change context representation
| -rw-r--r-- | src/codec.rs | 4 | ||||
| -rw-r--r-- | src/codec/decode.rs | 43 | ||||
| -rw-r--r-- | src/codec/encode.rs | 43 | ||||
| -rw-r--r-- | src/types/prefix/count.rs | 36 | ||||
| -rw-r--r-- | src/types/prefix/length.rs | 56 | ||||
| -rw-r--r-- | src/types/size.rs | 4 |
6 files changed, 75 insertions, 111 deletions
diff --git a/src/codec.rs b/src/codec.rs index fcb18a3..778001f 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -4,5 +4,5 @@ pub mod decode; pub mod encode; pub mod error; -pub trait Codec: Encode + Decode {} -impl<T> Codec for T where T: Encode + Decode {} +pub trait Codec<Ctx>: Encode<Ctx> + Decode<Ctx> {} +impl<Ctx, T> Codec<Ctx> for T where T: Encode<Ctx> + Decode<Ctx> {} 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) - } -} diff --git a/src/types/prefix/count.rs b/src/types/prefix/count.rs index cf541ec..5f53e08 100644 --- a/src/types/prefix/count.rs +++ b/src/types/prefix/count.rs @@ -2,14 +2,11 @@ use std::{io::Read, marker::PhantomData}; use crate::{ codec::{Codec, decode::Decode, encode::Encode}, - context::Context, types::size::Size, }; pub struct CountPrefix<I, L, D> where - I: Codec, - L: Codec + Size, D: IntoIterator<Item = I>, { data: D, @@ -18,8 +15,6 @@ where impl<I, L, D> Clone for CountPrefix<I, L, D> where - I: Codec, - L: Codec + Size, D: IntoIterator<Item = I> + Clone, { fn clone(&self) -> Self { @@ -32,8 +27,6 @@ where impl<I, L, D> CountPrefix<I, L, D> where - I: Codec, - L: Codec + Size, D: IntoIterator<Item = I>, { pub fn new(data: D) -> Self { @@ -46,8 +39,6 @@ where impl<I, L, D> AsRef<D> for CountPrefix<I, L, D> where - I: Codec, - L: Codec + Size, D: IntoIterator<Item = I>, { fn as_ref(&self) -> &D { @@ -57,8 +48,6 @@ where impl<I, L, D> AsMut<D> for CountPrefix<I, L, D> where - I: Codec, - L: Codec + Size, D: IntoIterator<Item = I>, { fn as_mut(&mut self) -> &mut D { @@ -66,22 +55,25 @@ where } } -impl<I, L, D> Encode for CountPrefix<I, L, D> +impl<I, L, D, Ctx> Encode<Ctx> for CountPrefix<I, L, D> where - I: Codec, - L: Codec + Size, + I: Codec<Ctx>, + L: Codec<Ctx> + Size + TryFrom<usize>, D: IntoIterator<Item = I> + Clone, { fn encode( &self, buffer: &mut dyn std::io::prelude::Write, - ctx: &dyn Context, + ctx: &mut Ctx, ) -> Result<usize, crate::codec::error::Error> where Self: Sized, { let vec = self.data.clone().into_iter().collect::<Vec<I>>(); - let mut l = L::from_size(vec.len()).encode(buffer, ctx)?; + let len: L = vec.len().try_into().map_err(|_| { + crate::codec::error::Error::Custom("count exceeds prefix capacity".into()) + })?; + let mut l = len.encode(buffer, ctx)?; for item in vec { l += item.encode(buffer, ctx)?; } @@ -89,24 +81,24 @@ where } } -impl<I, L, D> Decode for CountPrefix<I, L, D> +impl<I, L, D, Ctx> Decode<Ctx> for CountPrefix<I, L, D> where - I: Codec, - L: Codec + Size, + I: Codec<Ctx>, + L: Codec<Ctx> + Size, D: IntoIterator<Item = I> + FromIterator<I>, { - fn decode(reader: &mut dyn Read, ctx: &dyn Context) -> crate::codec::error::Result<Self> + fn decode(reader: &mut dyn Read, ctx: &mut Ctx) -> crate::codec::error::Result<Self> where Self: Sized, { - let len = L::decode(reader, ctx)?.as_size(); + let len = L::decode(reader, ctx)?.into_size(); let mut data = Vec::with_capacity(len); for _ in 0..len { let item = I::decode(reader, ctx)?; data.push(item); } Ok(Self { - data: D::from_iter(data.into_iter()), + data: D::from_iter(data), _len: PhantomData, }) } diff --git a/src/types/prefix/length.rs b/src/types/prefix/length.rs index fe159fc..160fffc 100644 --- a/src/types/prefix/length.rs +++ b/src/types/prefix/length.rs @@ -3,23 +3,17 @@ use std::{io::Write, marker::PhantomData}; use crate::{ DEFAULT_BUFFER_LEN, codec::{self, Codec, decode::Decode, encode::Encode}, - context::Context, types::size::Size, }; -pub struct LenPrefixed<L, D> -where - L: Codec + Size, - D: Codec, -{ +pub struct LenPrefixed<L, D> { data: D, _len: PhantomData<L>, } impl<L, D> Clone for LenPrefixed<L, D> where - L: Codec + Size, - D: Codec + Clone, + D: Clone, { fn clone(&self) -> Self { Self { @@ -29,11 +23,7 @@ where } } -impl<L, D> LenPrefixed<L, D> -where - L: Codec + Size, - D: Codec, -{ +impl<L, D> LenPrefixed<L, D> { pub fn new(data: D) -> Self { Self { data, @@ -42,57 +32,49 @@ where } } -impl<L, D> AsRef<D> for LenPrefixed<L, D> -where - L: Codec + Size, - D: Codec, -{ +impl<L, D> AsRef<D> for LenPrefixed<L, D> { fn as_ref(&self) -> &D { &self.data } } -impl<L, D> AsMut<D> for LenPrefixed<L, D> -where - L: Codec + Size, - D: Codec, -{ +impl<L, D> AsMut<D> for LenPrefixed<L, D> { fn as_mut(&mut self) -> &mut D { &mut self.data } } -impl<L, D> Encode for LenPrefixed<L, D> +impl<L, D, Ctx> Encode<Ctx> for LenPrefixed<L, D> where - L: Codec + Size, - D: Codec, + L: Codec<Ctx> + Size + TryFrom<usize>, + D: Codec<Ctx>, { - fn encode( - &self, - writer: &mut dyn Write, - ctx: &dyn Context, - ) -> Result<usize, codec::error::Error> { + fn encode(&self, writer: &mut dyn Write, ctx: &mut Ctx) -> Result<usize, codec::error::Error> { let mut buf = Vec::with_capacity(DEFAULT_BUFFER_LEN); self.data.encode(&mut buf, ctx)?; - let mut l = L::from_size(buf.len()).encode(writer, ctx)?; + let len: L = buf + .len() + .try_into() + .map_err(|_| codec::error::Error::Custom("length exceeds prefix capacity".into()))?; + let mut l = len.encode(writer, ctx)?; l += writer.write(&buf)?; Ok(l) } } -impl<L, D> Decode for LenPrefixed<L, D> +impl<L, D, Ctx> Decode<Ctx> for LenPrefixed<L, D> where - L: Codec + Size, - D: Codec, + L: Codec<Ctx> + Size, + D: Codec<Ctx>, { fn decode( reader: &mut dyn std::io::prelude::Read, - ctx: &dyn Context, + ctx: &mut Ctx, ) -> crate::codec::error::Result<Self> where Self: Sized, { - let len = L::decode(reader, ctx)?.as_size(); + let len = L::decode(reader, ctx)?.into_size(); let mut limited = vec![0_u8; len]; reader.read_exact(&mut limited)?; let data = D::decode(&mut &limited[..], ctx)?; diff --git a/src/types/size.rs b/src/types/size.rs index 6bce689..788d9af 100644 --- a/src/types/size.rs +++ b/src/types/size.rs @@ -5,7 +5,7 @@ macro_rules! impl_size { size as $t } - fn as_size(self) -> usize { + fn into_size(self) -> usize { self as usize } } @@ -14,7 +14,7 @@ macro_rules! impl_size { pub trait Size { fn from_size(size: usize) -> Self; - fn as_size(self) -> usize; + fn into_size(self) -> usize; } impl_size!(u8); |
