From 814c48863c6e98561cc2436d969225112106c454 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 24 Nov 2014 22:42:49 -0800 Subject: [PATCH] More duplication reduction --- src/types/mod.rs | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/src/types/mod.rs b/src/types/mod.rs index 0cf9193e..6ee6956f 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -121,6 +121,7 @@ macro_rules! from_array_impl( from_map_impl!($($oid)|+, ::types::array::ArrayBase>, |buf: &Vec| { use std::io::{BufReader, ByRefReader}; use std::io::util::LimitReader; + use std::iter::MultiplicativeIterator; use types::{Oid, RawFromSql}; use types::array::{ArrayBase, DimensionInfo}; use Error; @@ -138,7 +139,7 @@ macro_rules! from_array_impl( lower_bound: try!(rdr.read_be_i32()) as int }); } - let nele = dim_info.iter().fold(1, |acc, info| acc * info.len); + let nele = dim_info.iter().map(|info| info.len).product(); let mut elements = Vec::with_capacity(nele); for _ in range(0, nele) { @@ -175,49 +176,40 @@ macro_rules! to_range_impl( fn raw_to_sql(&self, buf: &mut W) -> Result<()> { use types::{RANGE_EMPTY, RANGE_LOWER_UNBOUNDED, RANGE_LOWER_INCLUSIVE, RANGE_UPPER_UNBOUNDED, RANGE_UPPER_INCLUSIVE}; - use types::range::{BoundType, RangeBound}; + use types::range::{BoundType, RangeBound, BoundSided}; let mut tag = 0; if self.is_empty() { tag |= RANGE_EMPTY; } else { - match self.lower() { - None => tag |= RANGE_LOWER_UNBOUNDED, - Some(&RangeBound { type_: BoundType::Inclusive, .. }) => { - tag |= RANGE_LOWER_INCLUSIVE + fn make_tag(bound: Option<&RangeBound>, unbounded_tag: i8, + inclusive_tag: i8) -> i8 where T: BoundSided { + match bound { + None => unbounded_tag, + Some(&RangeBound { type_: BoundType::Inclusive, .. }) => inclusive_tag, + _ => 0 } - _ => {} - } - match self.upper() { - None => tag |= RANGE_UPPER_UNBOUNDED, - Some(&RangeBound { type_: BoundType::Inclusive, .. }) => { - tag |= RANGE_UPPER_INCLUSIVE - } - _ => {} } + tag |= make_tag(self.lower(), RANGE_LOWER_UNBOUNDED, RANGE_LOWER_INCLUSIVE); + tag |= make_tag(self.upper(), RANGE_UPPER_UNBOUNDED, RANGE_UPPER_INCLUSIVE); } try!(buf.write_i8(tag)); - match self.lower() { - Some(bound) => { + fn write_value(buf: &mut W, v: Option<&RangeBound>) -> Result<()> + where T: BoundSided, W: Writer { + if let Some(bound) = v { let mut inner_buf = vec![]; try!(bound.value.raw_to_sql(&mut inner_buf)); - try!(buf.write_be_i32(inner_buf.len() as i32)); + try!(buf.write_be_u32(inner_buf.len() as u32)); try!(buf.write(inner_buf[])); } - None => {} - } - match self.upper() { - Some(bound) => { - let mut inner_buf = vec![]; - try!(bound.value.raw_to_sql(&mut inner_buf)); - try!(buf.write_be_i32(inner_buf.len() as i32)); - try!(buf.write(inner_buf[])); - } - None => {} + Ok(()) } + try!(write_value(buf, self.lower())); + try!(write_value(buf, self.upper())); + Ok(()) } }