Skip to content

Commit 814c488

Browse files
committed
More duplication reduction
1 parent a793bcc commit 814c488

1 file changed

Lines changed: 19 additions & 27 deletions

File tree

src/types/mod.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ macro_rules! from_array_impl(
121121
from_map_impl!($($oid)|+, ::types::array::ArrayBase<Option<$t>>, |buf: &Vec<u8>| {
122122
use std::io::{BufReader, ByRefReader};
123123
use std::io::util::LimitReader;
124+
use std::iter::MultiplicativeIterator;
124125
use types::{Oid, RawFromSql};
125126
use types::array::{ArrayBase, DimensionInfo};
126127
use Error;
@@ -138,7 +139,7 @@ macro_rules! from_array_impl(
138139
lower_bound: try!(rdr.read_be_i32()) as int
139140
});
140141
}
141-
let nele = dim_info.iter().fold(1, |acc, info| acc * info.len);
142+
let nele = dim_info.iter().map(|info| info.len).product();
142143

143144
let mut elements = Vec::with_capacity(nele);
144145
for _ in range(0, nele) {
@@ -175,49 +176,40 @@ macro_rules! to_range_impl(
175176
fn raw_to_sql<W: Writer>(&self, buf: &mut W) -> Result<()> {
176177
use types::{RANGE_EMPTY, RANGE_LOWER_UNBOUNDED, RANGE_LOWER_INCLUSIVE,
177178
RANGE_UPPER_UNBOUNDED, RANGE_UPPER_INCLUSIVE};
178-
use types::range::{BoundType, RangeBound};
179+
use types::range::{BoundType, RangeBound, BoundSided};
179180

180181
let mut tag = 0;
181182
if self.is_empty() {
182183
tag |= RANGE_EMPTY;
183184
} else {
184-
match self.lower() {
185-
None => tag |= RANGE_LOWER_UNBOUNDED,
186-
Some(&RangeBound { type_: BoundType::Inclusive, .. }) => {
187-
tag |= RANGE_LOWER_INCLUSIVE
185+
fn make_tag<T>(bound: Option<&RangeBound<T, $t>>, unbounded_tag: i8,
186+
inclusive_tag: i8) -> i8 where T: BoundSided {
187+
match bound {
188+
None => unbounded_tag,
189+
Some(&RangeBound { type_: BoundType::Inclusive, .. }) => inclusive_tag,
190+
_ => 0
188191
}
189-
_ => {}
190-
}
191-
match self.upper() {
192-
None => tag |= RANGE_UPPER_UNBOUNDED,
193-
Some(&RangeBound { type_: BoundType::Inclusive, .. }) => {
194-
tag |= RANGE_UPPER_INCLUSIVE
195-
}
196-
_ => {}
197192
}
193+
tag |= make_tag(self.lower(), RANGE_LOWER_UNBOUNDED, RANGE_LOWER_INCLUSIVE);
194+
tag |= make_tag(self.upper(), RANGE_UPPER_UNBOUNDED, RANGE_UPPER_INCLUSIVE);
198195
}
199196

200197
try!(buf.write_i8(tag));
201198

202-
match self.lower() {
203-
Some(bound) => {
204-
let mut inner_buf = vec![];
205-
try!(bound.value.raw_to_sql(&mut inner_buf));
206-
try!(buf.write_be_i32(inner_buf.len() as i32));
207-
try!(buf.write(inner_buf[]));
208-
}
209-
None => {}
210-
}
211-
match self.upper() {
212-
Some(bound) => {
199+
fn write_value<T, W>(buf: &mut W, v: Option<&RangeBound<T, $t>>) -> Result<()>
200+
where T: BoundSided, W: Writer {
201+
if let Some(bound) = v {
213202
let mut inner_buf = vec![];
214203
try!(bound.value.raw_to_sql(&mut inner_buf));
215-
try!(buf.write_be_i32(inner_buf.len() as i32));
204+
try!(buf.write_be_u32(inner_buf.len() as u32));
216205
try!(buf.write(inner_buf[]));
217206
}
218-
None => {}
207+
Ok(())
219208
}
220209

210+
try!(write_value(buf, self.lower()));
211+
try!(write_value(buf, self.upper()));
212+
221213
Ok(())
222214
}
223215
}

0 commit comments

Comments
 (0)