Skip to content

Commit 39b76df

Browse files
committed
Fix the empty array case
Closes sfackler#1
1 parent 52d3e40 commit 39b76df

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/impls/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ impl<T> RawFromSql for ArrayBase<Option<T>> where T: RawFromSql {
8282
lower_bound: try!(raw.read_be_i32()) as int,
8383
});
8484
}
85-
let nele = dim_info.iter().map(|info| info.len as uint).product();
85+
let nele = if dim_info.len() == 0 {
86+
0
87+
} else {
88+
dim_info.iter().map(|info| info.len as uint).product()
89+
};
8690

8791
let mut elements = Vec::with_capacity(nele);
8892
for _ in range(0, nele) {
@@ -160,6 +164,7 @@ mod test {
160164
use std::fmt;
161165

162166
use postgres::{Connection, SslMode, FromSql, ToSql};
167+
use ArrayBase;
163168

164169
fn test_type<T: PartialEq+FromSql+ToSql, S: fmt::Show>(sql_type: &str, checks: &[(T, S)]) {
165170
let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
@@ -176,7 +181,6 @@ mod test {
176181

177182
macro_rules! test_array_params {
178183
($name:expr, $v1:expr, $s1:expr, $v2:expr, $s2:expr, $v3:expr, $s3:expr) => ({
179-
use ArrayBase;
180184

181185
let tests = &[(Some(ArrayBase::from_vec(vec!(Some($v1), Some($v2), None), 1)),
182186
format!("'{{{},{},NULL}}'", $s1, $s2).into_string()),
@@ -256,4 +260,11 @@ mod test {
256260
fn test_float8array_params() {
257261
test_array_params!("FLOAT8", 0f64, "0", 1.5f64, "1.5", 0.009f64, ".009");
258262
}
263+
264+
#[test]
265+
fn test_empty_array() {
266+
let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
267+
let stmt = conn.prepare("SELECT '{}'::INT4[]").unwrap();
268+
stmt.query(&[]).unwrap().next().unwrap().get::<_, ArrayBase<Option<i32>>>(0);
269+
}
259270
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ impl<T> ArrayBase<T> {
9696
/// not match the number of elements specified.
9797
pub fn from_raw(data: Vec<T>, info: Vec<DimensionInfo>)
9898
-> ArrayBase<T> {
99-
assert!(!info.is_empty(), "Cannot create a 0x0 array");
100-
assert!(data.len() == info.iter().fold(1, |acc, i| acc * i.len),
99+
assert!((data.is_empty() && info.is_empty()) ||
100+
data.len() == info.iter().fold(1, |acc, i| acc * i.len),
101101
"Size mismatch");
102102
ArrayBase {
103103
info: info,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy