From 53692a63d7c868fda276806b249a3b7cb905e8cf Mon Sep 17 00:00:00 2001 From: Edd Robinson Date: Mon, 22 Jun 2020 11:37:29 +0100 Subject: [PATCH] refactor: DRY up Packers accessors --- delorean_table/src/packers.rs | 90 +++++++++++------------------------ 1 file changed, 28 insertions(+), 62 deletions(-) diff --git a/delorean_table/src/packers.rs b/delorean_table/src/packers.rs index 5b26d090a2..7ea92140c2 100644 --- a/delorean_table/src/packers.rs +++ b/delorean_table/src/packers.rs @@ -17,6 +17,28 @@ pub enum Packers { Boolean(Packer), } +macro_rules! typed_packer_accessors { + ($(($name:ident, $name_mut:ident, $type:ty, $variant:ident),)*) => { + $( + pub fn $name(&self) -> &Packer<$type> { + if let Self::$variant(p) = self { + p + } else { + panic!(concat!("packer is not a ", stringify!($variant))); + } + } + + pub fn $name_mut(&mut self) -> &mut Packer<$type> { + if let Self::$variant(p) = self { + p + } else { + panic!(concat!("packer is not a ", stringify!($variant))); + } + } + )* + }; +} + impl Packers { /// Reserves the minimum capacity for exactly additional more elements to /// be inserted into the Packer` without reallocation. @@ -68,68 +90,12 @@ impl Packers { } } - pub fn f64_packer_mut(&mut self) -> &mut Packer { - if let Self::Float(p) = self { - p - } else { - panic!("packer is not a Float"); - } - } - - pub fn f64_packer(&self) -> &Packer { - if let Self::Float(p) = self { - p - } else { - panic!("packer is not a Float"); - } - } - - pub fn i64_packer_mut(&mut self) -> &mut Packer { - if let Self::Integer(p) = self { - p - } else { - panic!("packer is not an Integer"); - } - } - - pub fn i64_packer(&self) -> &Packer { - if let Self::Integer(p) = self { - p - } else { - panic!("packer is not an Integer"); - } - } - - pub fn str_packer_mut(&mut self) -> &mut Packer { - if let Self::String(p) = self { - p - } else { - panic!("packer is not a String"); - } - } - - pub fn str_packer(&self) -> &Packer { - if let Self::String(p) = self { - p - } else { - panic!("packer is not a String"); - } - } - - pub fn bool_packer_mut(&mut self) -> &mut Packer { - if let Self::Boolean(p) = self { - p - } else { - panic!("packer is not a Boolean"); - } - } - - pub fn bool_packer(&self) -> &Packer { - if let Self::Boolean(p) = self { - p - } else { - panic!("packer is not a Boolean"); - } + // Implementations of all the accessors for the variants of `Packers`. + typed_packer_accessors! { + (f64_packer, f64_packer_mut, f64, Float), + (i64_packer, i64_packer_mut, i64, Integer), + (str_packer, str_packer_mut, ByteArray, String), + (bool_packer, bool_packer_mut, bool, Boolean), } }