Skip to content

Commit

Permalink
Merge pull request #8 from azam/feature/qualified-name-in-macros
Browse files Browse the repository at this point in the history
Canonicalize trait function call in macros
  • Loading branch information
azam authored Apr 18, 2023
2 parents d4b017e + 7011d09 commit 28beeb6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
28 changes: 16 additions & 12 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Read + Sized>(reader: &mut R) -> Result<Self> {
let size = mem::size_of::<Self>();
// Saving one call instead of calling below
// let size = mem::size_of::<Self>();
const SIZE: usize = $s;
let mut bytes = Vec::<u8>::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.
Expand All @@ -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((
$(
Expand Down Expand Up @@ -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((
$(
Expand Down
22 changes: 11 additions & 11 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: Write>(&self, writer: &mut W) -> Result<usize> {
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::<u8>::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".
Expand All @@ -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::<u8>::new();
$(
$values.azam_encode_write(&mut bytes).unwrap();
$crate::encode::AzamEncode::azam_encode_write(&$values, &mut bytes).unwrap();
)*
String::from_utf8(bytes).unwrap()
}};
Expand Down

0 comments on commit 28beeb6

Please sign in to comment.