From 9c37388c9f4be75c3d77a4019c92019dcda5ae93 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 23 Jul 2017 13:44:23 -0700 Subject: [PATCH 1/9] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1ae0889..0008029 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # rust-postgres-array -[![Build Status](https://travis-ci.org/sfackler/rust-postgres-array.svg?branch=master)](https://travis-ci.org/sfackler/rust-postgres-array) +[![CircleCI](https://circleci.com/gh/sfackler/rust-postgres-array.svg?style=shield)](https://circleci.com/gh/sfackler/rust-postgres-array) -[Documentation](https://sfackler.github.io/rust-postgres-array/doc/v0.7.1/postgres_array) +[Documentation](https://docs.rs/postgres_array) Support for PostgreSQL arrays in [rust-postgres](https://github.com/sfackler/rust-postgres). From c68b66f93755c6b4f24470e9997252161854bf39 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 12 Jan 2020 16:05:52 -0800 Subject: [PATCH 2/9] Upgrade to 2018 --- Cargo.toml | 1 + src/array.rs | 62 ++++++++++++++++++----------------- src/impls.rs | 91 ++++++++++++++++++++++++++-------------------------- src/lib.rs | 24 ++++---------- 4 files changed, 86 insertions(+), 92 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b2481cc..af06c0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "postgres_array" version = "0.9.0" authors = ["Steven Fackler "] +edition = "2018" license = "MIT" description = "Array support for rust-postgres" repository = "https://github.com/sfackler/rust-postgres-array" diff --git a/src/array.rs b/src/array.rs index 1cf85b3..3c030aa 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1,9 +1,9 @@ +use std::fmt; use std::ops::{Index, IndexMut}; use std::slice; use std::vec; -use std::fmt; -use Dimension; +use crate::Dimension; /// A multi-dimensional array. #[derive(Debug, PartialEq, Eq, Clone)] @@ -13,17 +13,17 @@ pub struct Array { } impl fmt::Display for Array { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { if self.dims.iter().any(|dim| dim.lower_bound != 1) { for dim in &self.dims { - try!(write!( + write!( fmt, "[{}:{}]", dim.lower_bound, dim.lower_bound + dim.len - 1 - )); + )?; } - try!(write!(fmt, "=")); + write!(fmt, "=")?; } fmt_helper(0, &self.dims, &mut self.data.iter(), fmt) } @@ -32,8 +32,8 @@ impl fmt::Display for Array { fn fmt_helper<'a, T, I>( depth: usize, dims: &[Dimension], - mut data: &mut I, - fmt: &mut fmt::Formatter, + data: &mut I, + fmt: &mut fmt::Formatter<'_>, ) -> fmt::Result where I: Iterator, @@ -43,12 +43,12 @@ where return write!(fmt, "{}", data.next().unwrap()); } - try!(write!(fmt, "{{")); + write!(fmt, "{{")?; for i in 0..dims[depth].len { if i != 0 { - try!(write!(fmt, ",")); + write!(fmt, ",")?; } - try!(fmt_helper(depth + 1, dims, data, fmt)); + fmt_helper(depth + 1, dims, data, fmt)?; } write!(fmt, "}}") } @@ -65,26 +65,24 @@ impl Array { /// elements specified by the dimensions. pub fn from_parts(data: Vec, dimensions: Vec) -> Array { assert!( - (data.is_empty() && dimensions.is_empty()) || - data.len() as i32 == dimensions.iter().fold(1, |acc, i| acc * i.len), + (data.is_empty() && dimensions.is_empty()) + || data.len() as i32 == dimensions.iter().fold(1, |acc, i| acc * i.len), "size mismatch" ); Array { dims: dimensions, - data: data, + data, } } /// Creates a new one-dimensional array. pub fn from_vec(data: Vec, lower_bound: i32) -> Array { Array { - dims: vec![ - Dimension { - len: data.len() as i32, - lower_bound: lower_bound, - }, - ], - data: data, + dims: vec![Dimension { + len: data.len() as i32, + lower_bound, + }], + data, } } @@ -97,7 +95,7 @@ impl Array { 0, Dimension { len: 1, - lower_bound: lower_bound, + lower_bound, }, ); } @@ -147,14 +145,18 @@ impl Array { /// Returns an iterator over references to the elements of the array in the /// higher-dimensional equivalent of row-major order. - pub fn iter<'a>(&'a self) -> Iter<'a, T> { - Iter { inner: self.data.iter() } + pub fn iter(&self) -> Iter<'_, T> { + Iter { + inner: self.data.iter(), + } } /// Returns an iterator over mutable references to the elements of the /// array in the higher-dimensional equivalent of row-major order. - pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> { - IterMut { inner: self.data.iter_mut() } + pub fn iter_mut(&mut self) -> IterMut<'_, T> { + IterMut { + inner: self.data.iter_mut(), + } } /// Returns the underlying data vector for this Array in the @@ -293,13 +295,15 @@ impl IntoIterator for Array { type IntoIter = IntoIter; fn into_iter(self) -> IntoIter { - IntoIter { inner: self.data.into_iter() } + IntoIter { + inner: self.data.into_iter(), + } } } /// An iterator over references to values of an `Array` in the /// higher-dimensional equivalent of row-major order. -pub struct Iter<'a, T: 'a> { +pub struct Iter<'a, T> { inner: slice::Iter<'a, T>, } @@ -329,7 +333,7 @@ impl<'a, T: 'a> ExactSizeIterator for Iter<'a, T> { /// An iterator over mutable references to values of an `Array` in the /// higher-dimensional equivalent of row-major order. -pub struct IterMut<'a, T: 'a> { +pub struct IterMut<'a, T> { inner: slice::IterMut<'a, T>, } diff --git a/src/impls.rs b/src/impls.rs index 2bf4f15..cb7c013 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -1,41 +1,36 @@ use fallible_iterator::FallibleIterator; -use postgres_shared::types::{Type, Kind, ToSql, FromSql, IsNull}; -use postgres_protocol::types; use postgres_protocol; +use postgres_protocol::types; +use postgres_shared::to_sql_checked; +use postgres_shared::types::{FromSql, IsNull, Kind, ToSql, Type}; use std::error::Error; -use {Array, Dimension}; +use crate::{Array, Dimension}; impl FromSql for Array where T: FromSql, { - fn from_sql(ty: &Type, raw: &[u8]) -> Result, Box> { + fn from_sql(ty: &Type, raw: &[u8]) -> Result, Box> { let element_type = match *ty.kind() { Kind::Array(ref ty) => ty, _ => unreachable!(), }; - let array = try!(types::array_from_sql(raw)); - - let dimensions = try!( - array - .dimensions() - .map(|d| { - Dimension { - len: d.len, - lower_bound: d.lower_bound, - } - }) - .collect() - ); + let array = types::array_from_sql(raw)?; - let elements = try!( - array - .values() - .and_then(|v| FromSql::from_sql_nullable(element_type, v)) - .collect() - ); + let dimensions = array + .dimensions() + .map(|d| Dimension { + len: d.len, + lower_bound: d.lower_bound, + }) + .collect()?; + + let elements = array + .values() + .and_then(|v| FromSql::from_sql_nullable(element_type, v)) + .collect()?; Ok(Array::from_parts(elements, dimensions)) } @@ -52,21 +47,19 @@ impl ToSql for Array where T: ToSql, { - fn to_sql(&self, ty: &Type, w: &mut Vec) -> Result> { + fn to_sql(&self, ty: &Type, w: &mut Vec) -> Result> { let element_type = match ty.kind() { &Kind::Array(ref ty) => ty, _ => unreachable!(), }; - let dimensions = self.dimensions().iter().map(|d| { - types::ArrayDimension { - len: d.len, - lower_bound: d.lower_bound, - } + let dimensions = self.dimensions().iter().map(|d| types::ArrayDimension { + len: d.len, + lower_bound: d.lower_bound, }); let elements = self.iter(); - try!(types::array_to_sql( + types::array_to_sql( dimensions, true, element_type.oid(), @@ -77,7 +70,7 @@ where Err(e) => Err(e), }, w, - )); + )?; Ok(IsNull::No) } @@ -96,18 +89,19 @@ where mod test { use std::fmt; - use postgres::{Connection, TlsMode}; + use crate::Array; use postgres::types::{FromSql, ToSql}; - use Array; + use postgres::{Connection, TlsMode}; fn test_type( sql_type: &str, checks: &[(T, S)], ) { - let conn = Connection::connect("postgres://postgres:password@localhost", TlsMode::None) - .unwrap(); + let conn = + Connection::connect("postgres://postgres:password@localhost", TlsMode::None).unwrap(); for &(ref val, ref repr) in checks.iter() { - let stmt = conn.prepare(&format!("SELECT {}::{}", *repr, sql_type)) + let stmt = conn + .prepare(&format!("SELECT {}::{}", *repr, sql_type)) .unwrap(); let result = stmt.query(&[]).unwrap().iter().next().unwrap().get(0); assert!(val == &result); @@ -119,19 +113,24 @@ mod test { } macro_rules! test_array_params { - ($name:expr, $v1:expr, $s1:expr, $v2:expr, $s2:expr, $v3:expr, $s3:expr) => ({ - - let tests = &[(Some(Array::from_vec(vec!(Some($v1), Some($v2), None), 1)), - format!("'{{{},{},NULL}}'", $s1, $s2)), - (None, "NULL".to_string())]; + ($name:expr, $v1:expr, $s1:expr, $v2:expr, $s2:expr, $v3:expr, $s3:expr) => {{ + let tests = &[ + ( + Some(Array::from_vec(vec![Some($v1), Some($v2), None], 1)), + format!("'{{{},{},NULL}}'", $s1, $s2), + ), + (None, "NULL".to_string()), + ]; test_type(&format!("{}[]", $name), tests); - let mut a = Array::from_vec(vec!(Some($v1), Some($v2)), 0); + let mut a = Array::from_vec(vec![Some($v1), Some($v2)], 0); a.wrap(-1); - a.push(Array::from_vec(vec!(None, Some($v3)), 0)); - let tests = &[(Some(a), format!("'[-1:0][0:1]={{{{{},{}}},{{NULL,{}}}}}'", - $s1, $s2, $s3))]; + a.push(Array::from_vec(vec![None, Some($v3)], 0)); + let tests = &[( + Some(a), + format!("'[-1:0][0:1]={{{{{},{}}},{{NULL,{}}}}}'", $s1, $s2, $s3), + )]; test_type(&format!("{}[][]", $name), tests); - }) + }}; } #[test] diff --git a/src/lib.rs b/src/lib.rs index a4b29af..75357eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,8 @@ //! Multi-dimensional arrays with per-dimension specifiable lower bounds -#![doc(html_root_url="https://docs.rs/postgres_array/0.9.0")] - -extern crate fallible_iterator; -#[macro_use] -extern crate postgres_shared; -extern crate postgres_protocol; - -#[cfg(test)] -extern crate postgres; +#![doc(html_root_url = "https://docs.rs/postgres_array/0.9.0")] #[doc(inline)] -pub use array::Array; +pub use crate::array::Array; pub mod array; mod impls; @@ -47,13 +39,11 @@ mod tests { fn test_from_vec() { let a = Array::from_vec(vec![0i32, 1, 2], -1); assert!( - &[ - Dimension { - len: 3, - lower_bound: -1, - }, - ] - [..] == a.dimensions() + &[Dimension { + len: 3, + lower_bound: -1, + },][..] + == a.dimensions() ); assert_eq!(0, a[-1]); assert_eq!(1, a[0]); From a2f4b5c29dae81a02a6b25f4da58587a58bbf33f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 12 Jan 2020 16:20:51 -0800 Subject: [PATCH 3/9] Upgrade to new postgres version --- Cargo.toml | 9 +++++---- circle.yml | 13 ++++++------ src/impls.rs | 56 ++++++++++++++++++++++++---------------------------- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index af06c0e..6721ca1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,10 @@ repository = "https://github.com/sfackler/rust-postgres-array" documentation = "https://docs.rs/postgres_array/0.9.0/postgres_array" [dependencies] -fallible-iterator = "0.1" -postgres-shared = "0.4" -postgres-protocol = "0.3" +bytes = "0.5" +fallible-iterator = "0.2" +postgres-types = "0.1" +postgres-protocol = "0.5" [dev-dependencies] -postgres = "0.15" +postgres = "0.17" diff --git a/circle.yml b/circle.yml index a648ab0..290a005 100644 --- a/circle.yml +++ b/circle.yml @@ -1,10 +1,9 @@ version: 2 jobs: build: - working_directory: ~/build docker: - - image: jimmycuadra/rust:1.19.0 - - image: postgres:9.6 + - image: rust:1.40.0 + - image: postgres:12 environment: POSTGRES_PASSWORD: password steps: @@ -15,12 +14,12 @@ jobs: - save_cache: key: registry-{{ epoch }} paths: - - ~/.cargo/registry/index + - /usr/local/cargo/registry/index - restore_cache: - key: dependencies-1.19-{{ checksum "Cargo.lock" }} + key: dependencies-1.40-{{ checksum "Cargo.lock" }} - run: cargo test - save_cache: - key: dependencies-1.19-{{ checksum "Cargo.lock" }} + key: dependencies-1.40-{{ checksum "Cargo.lock" }} paths: - target - - ~/.cargo/registry/cache + - /usr/local/cargo/registry/cache diff --git a/src/impls.rs b/src/impls.rs index cb7c013..399a36e 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -1,17 +1,17 @@ use fallible_iterator::FallibleIterator; use postgres_protocol; use postgres_protocol::types; -use postgres_shared::to_sql_checked; -use postgres_shared::types::{FromSql, IsNull, Kind, ToSql, Type}; +use postgres_types::{to_sql_checked, FromSql, IsNull, Kind, ToSql, Type}; use std::error::Error; use crate::{Array, Dimension}; +use postgres_types::private::BytesMut; -impl FromSql for Array +impl<'de, T> FromSql<'de> for Array where - T: FromSql, + T: FromSql<'de>, { - fn from_sql(ty: &Type, raw: &[u8]) -> Result, Box> { + fn from_sql(ty: &Type, raw: &'de [u8]) -> Result, Box> { let element_type = match *ty.kind() { Kind::Array(ref ty) => ty, _ => unreachable!(), @@ -21,15 +21,17 @@ where let dimensions = array .dimensions() - .map(|d| Dimension { - len: d.len, - lower_bound: d.lower_bound, + .map(|d| { + Ok(Dimension { + len: d.len, + lower_bound: d.lower_bound, + }) }) .collect()?; let elements = array .values() - .and_then(|v| FromSql::from_sql_nullable(element_type, v)) + .map(|v| FromSql::from_sql_nullable(element_type, v)) .collect()?; Ok(Array::from_parts(elements, dimensions)) @@ -47,7 +49,7 @@ impl ToSql for Array where T: ToSql, { - fn to_sql(&self, ty: &Type, w: &mut Vec) -> Result> { + fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result> { let element_type = match ty.kind() { &Kind::Array(ref ty) => ty, _ => unreachable!(), @@ -61,7 +63,6 @@ where types::array_to_sql( dimensions, - true, element_type.oid(), elements, |v, w| match v.to_sql(element_type, w) { @@ -90,24 +91,25 @@ mod test { use std::fmt; use crate::Array; - use postgres::types::{FromSql, ToSql}; - use postgres::{Connection, TlsMode}; + use postgres::types::{FromSqlOwned, ToSql}; + use postgres::{Client, NoTls}; - fn test_type( + fn test_type( sql_type: &str, checks: &[(T, S)], ) { - let conn = - Connection::connect("postgres://postgres:password@localhost", TlsMode::None).unwrap(); + let mut conn = Client::connect("postgres://postgres:password@localhost", NoTls).unwrap(); for &(ref val, ref repr) in checks.iter() { - let stmt = conn - .prepare(&format!("SELECT {}::{}", *repr, sql_type)) - .unwrap(); - let result = stmt.query(&[]).unwrap().iter().next().unwrap().get(0); + let result = conn + .query(&*format!("SELECT {}::{}", *repr, sql_type), &[]) + .unwrap()[0] + .get(0); assert!(val == &result); - let stmt = conn.prepare(&format!("SELECT $1::{}", sql_type)).unwrap(); - let result = stmt.query(&[val]).unwrap().iter().next().unwrap().get(0); + let result = conn + .query(&*format!("SELECT $1::{}", sql_type), &[val]) + .unwrap()[0] + .get(0); assert!(val == &result); } } @@ -235,13 +237,7 @@ mod test { #[test] fn test_empty_array() { - let conn = Connection::connect("postgres://postgres@localhost", TlsMode::None).unwrap(); - let stmt = conn.prepare("SELECT '{}'::INT4[]").unwrap(); - stmt.query(&[]) - .unwrap() - .iter() - .next() - .unwrap() - .get::<_, Array>(0); + let mut conn = Client::connect("postgres://postgres@localhost", NoTls).unwrap(); + conn.query("SELECT '{}'::INT4[]", &[]).unwrap()[0].get::<_, Array>(0); } } From c7d3f31f9f2bfbc8155a52c6a75f4687eef6c3cd Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 12 Jan 2020 16:25:06 -0800 Subject: [PATCH 4/9] Release v0.10.0 --- Cargo.toml | 3 +-- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6721ca1..ed1be43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,11 @@ [package] name = "postgres_array" -version = "0.9.0" +version = "0.10.0" authors = ["Steven Fackler "] edition = "2018" license = "MIT" description = "Array support for rust-postgres" repository = "https://github.com/sfackler/rust-postgres-array" -documentation = "https://docs.rs/postgres_array/0.9.0/postgres_array" [dependencies] bytes = "0.5" diff --git a/src/lib.rs b/src/lib.rs index 75357eb..a326a53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ //! Multi-dimensional arrays with per-dimension specifiable lower bounds -#![doc(html_root_url = "https://docs.rs/postgres_array/0.9.0")] +#![doc(html_root_url = "https://docs.rs/postgres_array/0.10")] #[doc(inline)] pub use crate::array::Array; From 014e740ae5c3c4bcb6bfae8b8790eb4e73be9f32 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Wed, 6 Jan 2021 01:06:33 -0500 Subject: [PATCH 5/9] Upgrade to new postgres version --- Cargo.toml | 8 ++++---- circle.yml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ed1be43..e0e9682 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,10 @@ description = "Array support for rust-postgres" repository = "https://github.com/sfackler/rust-postgres-array" [dependencies] -bytes = "0.5" +bytes = "1.0" fallible-iterator = "0.2" -postgres-types = "0.1" -postgres-protocol = "0.5" +postgres-types = "0.2" +postgres-protocol = "0.6" [dev-dependencies] -postgres = "0.17" +postgres = "0.19" diff --git a/circle.yml b/circle.yml index 290a005..23f9e02 100644 --- a/circle.yml +++ b/circle.yml @@ -2,7 +2,7 @@ version: 2 jobs: build: docker: - - image: rust:1.40.0 + - image: rust:1.45.0 - image: postgres:12 environment: POSTGRES_PASSWORD: password From fd0cac7df5e46dbe4e1364eebf28627e1a028353 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 6 Jan 2021 08:08:19 -0500 Subject: [PATCH 6/9] Release v0.11.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e0e9682..c03eb0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "postgres_array" -version = "0.10.0" +version = "0.11.0" authors = ["Steven Fackler "] edition = "2018" license = "MIT" From 00c305670b5537e0109547ab969d3e23fa8216d7 Mon Sep 17 00:00:00 2001 From: Joseph Koshakow Date: Thu, 13 Oct 2022 18:43:27 -0400 Subject: [PATCH 7/9] Fix display for 0 dimensional arrays --- src/array.rs | 4 ++++ src/lib.rs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/array.rs b/src/array.rs index 3c030aa..9886d8d 100644 --- a/src/array.rs +++ b/src/array.rs @@ -39,6 +39,10 @@ where I: Iterator, T: 'a + fmt::Display, { + if dims.len() == 0 { + return write!(fmt, "{{}}"); + } + if depth == dims.len() { return write!(fmt, "{}", data.next().unwrap()); } diff --git a/src/lib.rs b/src/lib.rs index a326a53..21240da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,5 +146,8 @@ mod tests { a.push(Array::from_vec(vec![4, 5, 6], 3)); a.wrap(1); assert_eq!("[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}", &format!("{}", a)); + + let a: Array = Array::from_parts(vec![], vec![]); + assert_eq!("{}", &format!("{}", a)); } } From 830bd609dda5a0d7e49256e2d059a9a6a2fedd4e Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 16 Oct 2022 20:55:41 -0400 Subject: [PATCH 8/9] bump ci version --- circle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circle.yml b/circle.yml index 23f9e02..8e1b563 100644 --- a/circle.yml +++ b/circle.yml @@ -2,7 +2,7 @@ version: 2 jobs: build: docker: - - image: rust:1.45.0 + - image: rust:1.64.0 - image: postgres:12 environment: POSTGRES_PASSWORD: password From a3b83ff88e848167364c0c797af9e9c487b7ed95 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 16 Oct 2022 20:59:08 -0400 Subject: [PATCH 9/9] Release v0.11.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c03eb0d..5fa0f65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "postgres_array" -version = "0.11.0" +version = "0.11.1" authors = ["Steven Fackler "] edition = "2018" license = "MIT"