summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/vrrpv2.rs28
1 files changed, 14 insertions, 14 deletions
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<u8> {
- let mut buffer = [0; size_of::<u8>()];
- cursor.read_exact(&mut buffer)?;
- Ok(u8::from_be_bytes(buffer))
-}
-
-fn read_u16(cursor: &mut Cursor<&[u8]>) -> io::Result<u16> {
- let mut buffer = [0; size_of::<u16>()];
- 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<u32> {
- let mut buffer = [0; size_of::<u32>()];
- 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<VRRPv2, VRRPv2Error> {