From c8a5faa1e937d6437c42baec36a904de9704b971 Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Mon, 21 Jul 2025 21:32:08 -0400 Subject: [PATCH] Removed anyhow dependency --- Cargo.toml | 1 - poppable-derive/src/lib.rs | 48 ++------------------------------------ src/lib.rs | 38 ++++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9a71e5a..683f085 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.81" poppable-derive = { path = "poppable-derive"} diff --git a/poppable-derive/src/lib.rs b/poppable-derive/src/lib.rs index bf4deab..fb7c8bb 100644 --- a/poppable-derive/src/lib.rs +++ b/poppable-derive/src/lib.rs @@ -48,7 +48,7 @@ macro_rules! pop_derive_macro { quote!( impl $trait_name for #struct_ident { - fn $trait_pop_fn(mut source: &mut &[u8]) -> anyhow::Result + fn $trait_pop_fn(mut source: &mut &[u8]) -> std::io::Result where Self: Sized, { @@ -57,7 +57,7 @@ macro_rules! pop_derive_macro { #(#names),* }) } - fn $trait_push_fn(&self, dest: &mut T) -> anyhow::Result<()> { + fn $trait_push_fn(&self, dest: &mut T) -> std::io::Result<()> { #(#pop)* Ok(()) } @@ -70,47 +70,3 @@ macro_rules! pop_derive_macro { pop_derive_macro!(pop_ne_derive_macro, PopFromNE, pop_ne_from, push_ne_into); pop_derive_macro!(pop_le_derive_macro, PopFromLE, pop_le_from, push_le_into); pop_derive_macro!(pop_be_derive_macro, PopFromBE, pop_be_from, push_be_into); -/* -#[proc_macro_derive(PopFromNE)] -pub fn pop_ne_derive_macro(item: TokenStream) -> TokenStream { - let ast: DeriveInput = syn::parse(item).unwrap(); - let struct_ident = ast.ident; - let struct_data: syn::DataStruct = match ast.data { - syn::Data::Struct(struct_data) => struct_data, - syn::Data::Enum(_) => panic!("Error: Enum support not implemented"), - syn::Data::Union(_) => panic!("Error: Union support not implemented"), - }; - - let recursive = struct_data.fields.iter().map(|f| { - let name = &f.ident; - let ty = &f.ty; - quote_spanned!(f.span()=> let #name = <#ty>::pop_ne_from(&mut source)?;) - }); - - let names = struct_data.fields.iter().map(|f| { - let name = &f.ident; - quote_spanned!(f.span()=> #name) - }); - let pop = struct_data.fields.iter().map(|f| { - let name = &f.ident; - quote_spanned!(f.span()=> PopFromNE::push_ne_into(&self.#name, dest)?;) - }); - - quote!( - impl PopFromNE for #struct_ident { - fn pop_ne_from(mut source: &mut &[u8]) -> anyhow::Result - where - Self: Sized, - { - #(#recursive)* - Ok(#struct_ident{ - #(#names),* - }) - } - fn push_ne_into(&self, dest: &mut T) -> anyhow::Result<()> { - #(#pop)* - Ok(()) - } - }) - .into() -}*/ diff --git a/src/lib.rs b/src/lib.rs index 01ba0f2..23a6895 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ -use anyhow::{Ok, Result}; +//use anyhow::{Ok, Result}; pub use poppable_derive::PushPopParse; +use std::io::{self, Result}; //pub type BytesRef = AsRef; @@ -79,7 +80,10 @@ impl PopFromNE for u8 { { const SIZE: usize = std::mem::size_of::(); if source.len() < SIZE { - anyhow::bail!("Buffer too small to pop u8"); + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + "Buffer too small to pop u8", + )); } let value = source[0]; @@ -126,9 +130,12 @@ macro_rules! core_impl_pop_ne { { const SIZE: usize = std::mem::size_of::<$int_type>(); if source.len() < SIZE { - anyhow::bail!("Buffer too small to pop {}", stringify!($int_type)); + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + stringify!(Buffer too small to pop $int_type), + )); } - let value = $int_type::from_ne_bytes(source[..SIZE].try_into()?); + let value = $int_type::from_ne_bytes(source[..SIZE].try_into().unwrap()); *source = &source[SIZE..]; Ok(value) } @@ -176,9 +183,12 @@ macro_rules! flt_impl_pop_non_ne { { const SIZE: usize = std::mem::size_of::<$flt_type>(); if source.len() < SIZE { - anyhow::bail!("Buffer to small to pop $flt_type"); + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + stringify!(Buffer too small to pop $flt_type), + )); } - let value = $flt_type::from_le_bytes(source[..SIZE].try_into()?); + let value = $flt_type::from_le_bytes(source[..SIZE].try_into().unwrap()); *source = &source[SIZE..]; Ok(value) } @@ -194,9 +204,12 @@ macro_rules! flt_impl_pop_non_ne { { const SIZE: usize = std::mem::size_of::<$flt_type>(); if source.len() < SIZE { - anyhow::bail!("Buffer to small to pop $flt_type"); + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + stringify!(Buffer too small to pop $flt_type), + )); } - let value = $flt_type::from_be_bytes(source[..SIZE].try_into()?); + let value = $flt_type::from_be_bytes(source[..SIZE].try_into().unwrap()); *source = &source[SIZE..]; Ok(value) } @@ -242,9 +255,14 @@ impl PopFromNE for [u8; SIZE] { Self: Sized, { if source.len() < SIZE { - anyhow::bail!("Buffer to small to pop [u8; {SIZE}]"); + //anyhow::bail!("Buffer to small to pop [u8; {SIZE}]"); + return Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + format!("Buffer too small to pop [u8; {SIZE}]"), + //stringify!(Buffer too small to pop $flt_type), + )); } - let value = source[..SIZE].try_into()?; + let value = source[..SIZE].try_into().unwrap(); *source = &source[SIZE..]; Ok(value) }