diff options
| author | zirkonya <zirkonya@iridium.lan> | 2026-09-01 18:09:21 +0200 |
|---|---|---|
| committer | zirkonya <zirkonya@iridium.lan> | 2026-09-01 18:09:21 +0200 |
| commit | 83176dcada1e4691c0d08dca49acf154eac4fd40 (patch) | |
| tree | 559cd884ac3663bbd09f00a980b6f941a4dd284b | |
| parent | ad95589a8a01ec1ca1ff8826d457691f94da6770 (diff) | |
Change Size trait to allow isizemain
| -rw-r--r-- | src/types/prefix/count.rs | 2 | ||||
| -rw-r--r-- | src/types/prefix/length.rs | 4 | ||||
| -rw-r--r-- | src/types/size.rs | 46 |
3 files changed, 33 insertions, 19 deletions
diff --git a/src/types/prefix/count.rs b/src/types/prefix/count.rs index f8ac68d..5856a72 100644 --- a/src/types/prefix/count.rs +++ b/src/types/prefix/count.rs @@ -94,7 +94,7 @@ where where Self: Sized, { - let len = L::decode(buf, ctx)?.into_size(); + let len = L::decode(buf, ctx)?.into_usize(); let mut data = Vec::with_capacity(len); for _ in 0..len { let item = I::decode(buf, ctx)?; diff --git a/src/types/prefix/length.rs b/src/types/prefix/length.rs index fe839d5..75c4f1b 100644 --- a/src/types/prefix/length.rs +++ b/src/types/prefix/length.rs @@ -61,7 +61,7 @@ where let mut len = 0; let mut inner_buf = Vec::with_capacity(64); self.data.encode(&mut inner_buf, ctx)?; - let len_val = L::from_size(inner_buf.len()); + let len_val = L::from_usize(inner_buf.len()); len += len_val.encode(buffer, ctx)?; buffer.extend_from_slice(&inner_buf); len += inner_buf.len(); @@ -78,7 +78,7 @@ where where Self: Sized, { - let len = L::decode(buf, ctx)?.into_size(); + let len = L::decode(buf, ctx)?.into_usize(); if buf.len() < len { return Err(crate::codec::error::CodecError::IoError( std::io::Error::new( diff --git a/src/types/size.rs b/src/types/size.rs index 788d9af..8295b56 100644 --- a/src/types/size.rs +++ b/src/types/size.rs @@ -1,30 +1,44 @@ macro_rules! impl_size { - ($t: ty) => { + ($t: ty, $bits: expr) => { impl Size for $t { - fn from_size(size: usize) -> Self { + const BITS: usize = $bits; + + fn from_usize(size: usize) -> Self { size as $t } - fn into_size(self) -> usize { + fn into_usize(self) -> usize { self as usize } + + fn from_isize(size: isize) -> Self { + size as $t + } + + fn into_isize(self) -> isize { + self as isize + } } }; } pub trait Size { - fn from_size(size: usize) -> Self; - fn into_size(self) -> usize; + const BITS: usize; + + fn from_usize(size: usize) -> Self; + fn into_usize(self) -> usize; + fn from_isize(size: isize) -> Self; + fn into_isize(self) -> isize; } -impl_size!(u8); -impl_size!(u16); -impl_size!(u32); -impl_size!(u64); -impl_size!(u128); - -impl_size!(i8); -impl_size!(i16); -impl_size!(i32); -impl_size!(i64); -impl_size!(i128); +impl_size!(u8, 8); +impl_size!(u16, 16); +impl_size!(u32, 32); +impl_size!(u64, 64); +impl_size!(u128, 128); + +impl_size!(i8, 8); +impl_size!(i16, 16); +impl_size!(i32, 32); +impl_size!(i64, 64); +impl_size!(i128, 128); |
