Content-Length: 395272 | pFad | http://github.com/psqlpy-python/rust-postgres-array/commit/8fde724ad380c8ddffb38d547b37e43099d13834

9F Make Debug output match Postgres · psqlpy-python/rust-postgres-array@8fde724 · GitHub
Skip to content

Commit 8fde724

Browse files
committed
Make Debug output match Postgres
1 parent f81f0cf commit 8fde724

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/array.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
11
use std::ops::{Index, IndexMut};
22
use std::slice;
33
use std::vec;
4+
use std::fmt;
45

56
use Dimension;
67

78
//github.com/ A multi-dimensional array.
8-
#[derive(Debug, PartialEq, Eq, Clone)]
9+
#[derive(PartialEq, Eq, Clone)]
910
pub struct Array<T> {
1011
dims: Vec<Dimension>,
1112
data: Vec<T>,
1213
}
1314

15+
impl<T: fmt::Debug> fmt::Debug for Array<T> {
16+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
17+
if self.dims.iter().any(|dim| dim.lower_bound != 1) {
18+
for dim in &self.dims {
19+
try!(write!(fmt, "[{}:{}]", dim.lower_bound,
20+
dim.lower_bound + dim.len as isize - 1));
21+
}
22+
try!(write!(fmt, "="));
23+
}
24+
fmt_helper(0, &self.dims, &mut self.data.iter(), fmt)
25+
}
26+
}
27+
28+
fn fmt_helper<'a, T, I>(depth: usize,
29+
dims: &[Dimension],
30+
mut data: &mut I,
31+
fmt: &mut fmt::Formatter)
32+
-> fmt::Result
33+
where I: Iterator<Item=&'a T>, T: 'a+fmt::Debug {
34+
if depth == dims.len() {
35+
return write!(fmt, "{:?}", data.next().unwrap());
36+
}
37+
38+
try!(write!(fmt, "{{"));
39+
for i in 0..dims[depth].len {
40+
if i != 0 {
41+
try!(write!(fmt, ","));
42+
}
43+
try!(fmt_helper(depth + 1, dims, data, fmt));
44+
}
45+
write!(fmt, "}}")
46+
}
47+
1448
impl<T> Array<T> {
1549
//github.com/ Creates a new `Array` from its underlying components.
1650
//github.com/

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,19 @@ mod tests {
118118
a[(0, 0)] = 3;
119119
assert_eq!(3, a[(0, 0)]);
120120
}
121+
122+
#[test]
123+
fn test_debug() {
124+
let a = Array::from_vec(vec![0i32, 1, 2, 3, 4], 1);
125+
assert_eq!("{0,1,2,3,4}", &format!("{:?}", a));
126+
127+
let a = Array::from_vec(vec![0i32, 1, 2, 3, 4], -3);
128+
assert_eq!("[-3:1]={0,1,2,3,4}", &format!("{:?}", a));
129+
130+
let mut a = Array::from_vec(vec![1i32, 2, 3], 3);
131+
a.wrap(-2);
132+
a.push(Array::from_vec(vec![4, 5, 6], 3));
133+
a.wrap(1);
134+
assert_eq!("[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}", &format!("{:?}", a));
135+
}
121136
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/psqlpy-python/rust-postgres-array/commit/8fde724ad380c8ddffb38d547b37e43099d13834

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy