diff --git a/src/decode.rs b/src/decode.rs index ecd9374..e6c8461 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -250,36 +250,38 @@ impl AzamDecode for u8 { } macro_rules! azam_decode_uint_impl { - ($t:ty) => { + ($t:ty, $s:expr) => { impl AzamDecode for $t { fn azam_decode_read(reader: &mut R) -> Result { - let size = mem::size_of::(); + // Saving one call instead of calling below + // let size = mem::size_of::(); + const SIZE: usize = $s; let mut bytes = Vec::::new(); // Limit to twice the byte size of type - azam_decode_read_until(reader, &mut bytes, (size * 2) as u64)?; + $crate::decode::azam_decode_read_until(reader, &mut bytes, (SIZE * 2) as u64)?; // Extend byte array to byte size of type and prepend with zeroes let original_len = bytes.len(); - if original_len < size { - bytes.resize(size, 0); - bytes.rotate_right(size - original_len); + if original_len < SIZE { + bytes.resize(SIZE, 0); + bytes.rotate_right(SIZE - original_len); } - Ok(Self::from_be_bytes(bytes[..size].try_into().unwrap())) + Ok(Self::from_be_bytes(bytes[..SIZE].try_into().unwrap())) } } }; } -azam_decode_uint_impl!(u16); -azam_decode_uint_impl!(u32); -azam_decode_uint_impl!(u64); -azam_decode_uint_impl!(u128); +azam_decode_uint_impl!(u16, 2); +azam_decode_uint_impl!(u32, 4); +azam_decode_uint_impl!(u64, 8); +azam_decode_uint_impl!(u128, 16); /// Macro to decode Azam encoded string to tuples of any types that implements the [`AzamDecode`] trait /// /// # Examples /// /// ```rust -/// use azamcodec::{azam_decode, decode::AzamDecode}; +/// use azamcodec::azam_decode; /// // Decode multiple sections of Azam-encoded string to a tuple using azam_decode! macro. /// // "xytxvyyf" decodes to 0xdeadbeefu32. /// // "h5" decodes to 0x15u8. @@ -292,6 +294,7 @@ macro_rules! azam_decode { ($r:expr) => {Result::<()>::Ok(())}; ($r:expr $(,$t:ty)*) => { 'block: { + use $crate::decode::AzamDecode; let reader = &mut $r.as_bytes(); Ok(( $( @@ -324,6 +327,7 @@ macro_rules! azam_decode_read { ($r:expr) => {Result::<()>::Ok(())}; ($r:expr $(,$t:ty)*) => { 'block: { + use $crate::decode::AzamDecode; let reader = $r; Ok(( $( diff --git a/src/encode.rs b/src/encode.rs index 8217420..84b325e 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -211,34 +211,34 @@ pub trait AzamEncode { fn azam_encode(&self) -> String; } -macro_rules! azam_encode_impl { +macro_rules! azam_encode_uint_impl { ($t:ty) => { impl AzamEncode for $t { fn azam_encode_write(&self, writer: &mut W) -> Result { - azam_encode_write(&mut self.to_be_bytes().as_ref(), writer) + $crate::encode::azam_encode_write(&mut self.to_be_bytes().as_ref(), writer) } fn azam_encode(&self) -> String { let mut bytes = Vec::::new(); - self.azam_encode_write(&mut bytes).unwrap(); + $crate::encode::AzamEncode::azam_encode_write(self, &mut bytes).unwrap(); String::from_utf8(bytes).unwrap() } } }; } -azam_encode_impl!(u8); -azam_encode_impl!(u16); -azam_encode_impl!(u32); -azam_encode_impl!(u64); -azam_encode_impl!(u128); +azam_encode_uint_impl!(u8); +azam_encode_uint_impl!(u16); +azam_encode_uint_impl!(u32); +azam_encode_uint_impl!(u64); +azam_encode_uint_impl!(u128); /// Macro to encode tuples of any types that implements the [`AzamEncode`] trait to Azam codec encoded string. /// /// # Examples /// /// ```rust -/// use azamcodec::{azam_encode, encode::AzamEncode}; +/// use azamcodec::azam_encode; /// // Encode multiple values as Azam-encoded string, using azam_encode! macro. /// // 0xdeadbeefu32 encodes to "xytxvyyf". /// // 0x15u8 encodes to "h5". @@ -249,12 +249,12 @@ azam_encode_impl!(u128); macro_rules! azam_encode { () => {}; ($value:expr) => {{ - $value.azam_encode() + $crate::encode::AzamEncode::azam_encode(&$value) }}; ($($values:expr),*) => {{ let mut bytes = Vec::::new(); $( - $values.azam_encode_write(&mut bytes).unwrap(); + $crate::encode::AzamEncode::azam_encode_write(&$values, &mut bytes).unwrap(); )* String::from_utf8(bytes).unwrap() }};