From b5817130cf2869e70856510cc3dc424503df8d4f Mon Sep 17 00:00:00 2001 From: Sunil Nimmagadda Date: Mon, 29 Sep 2025 01:34:52 +0530 Subject: Replace repetitive code with a macro_rules! --- src/vrrpv2.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/vrrpv2.rs b/src/vrrpv2.rs index d226420..3e714fa 100644 --- a/src/vrrpv2.rs +++ b/src/vrrpv2.rs @@ -105,22 +105,22 @@ impl VRRPv2 { } } -fn read_u8(cursor: &mut Cursor<&[u8]>) -> io::Result { - let mut buffer = [0; size_of::()]; - cursor.read_exact(&mut buffer)?; - Ok(u8::from_be_bytes(buffer)) -} - -fn read_u16(cursor: &mut Cursor<&[u8]>) -> io::Result { - let mut buffer = [0; size_of::()]; - cursor.read_exact(&mut buffer)?; - Ok(u16::from_be_bytes(buffer)) +macro_rules! read_be { + ( $( $fname:ident => $type:ty ),+ $(,)? ) => { + $( + fn $fname(cursor: &mut Cursor<&[u8]>) -> io::Result<$type> { + let mut buffer = [0; size_of::<$type>()]; + cursor.read_exact(&mut buffer)?; + Ok(<$type>::from_be_bytes(buffer)) + } + )+ + }; } -fn read_u32(cursor: &mut Cursor<&[u8]>) -> io::Result { - let mut buffer = [0; size_of::()]; - cursor.read_exact(&mut buffer)?; - Ok(u32::from_be_bytes(buffer)) +read_be! { + read_u8 => u8, + read_u16 => u16, + read_u32 => u32, } fn parse(bytes: &[u8]) -> Result { -- cgit v1.2.3