summaryrefslogtreecommitdiff
path: root/src/codec/decode.rs
diff options
context:
space:
mode:
authorzirkonya <zirkonya@iridium.lan>2026-08-16 15:01:43 +0200
committerzirkonya <zirkonya@iridium.lan>2026-08-16 15:01:43 +0200
commit7297bbb864778814ed59e99d0d6b05853704f06d (patch)
treeb685e39344de04dc9ef6c103d11ec0d50d962cb1 /src/codec/decode.rs
parent7cfd669783c310747debcfca7106e6162f492340 (diff)
change context representation
Diffstat (limited to 'src/codec/decode.rs')
-rw-r--r--src/codec/decode.rs43
1 files changed, 21 insertions, 22 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,
{