diff --git a/.circleci/config.yml b/.circleci/config.yml index 47efb05d..9ea84498 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -22,7 +22,7 @@ version: 2 jobs: build: docker: - - image: rust:1.30.1 + - image: rust:1.31.0 environment: RUSTFLAGS: -D warnings - image: sfackler/rust-postgres-test:5 diff --git a/postgres-protocol/Cargo.toml b/postgres-protocol/Cargo.toml index 72194ac7..2c8157e5 100644 --- a/postgres-protocol/Cargo.toml +++ b/postgres-protocol/Cargo.toml @@ -2,6 +2,7 @@ name = "postgres-protocol" version = "0.3.2" authors = ["Steven Fackler "] +edition = "2018" description = "Low level Postgres protocol APIs" license = "MIT/Apache-2.0" repository = "https://github.com/sfackler/rust-postgres-protocol" diff --git a/postgres-protocol/src/authentication/sasl.rs b/postgres-protocol/src/authentication/sasl.rs index cd46d67d..8488fa3c 100644 --- a/postgres-protocol/src/authentication/sasl.rs +++ b/postgres-protocol/src/authentication/sasl.rs @@ -1,6 +1,5 @@ //! SASL-based authentication support. -use base64; use generic_array::typenum::U32; use generic_array::GenericArray; use hmac::{Hmac, Mac}; @@ -11,7 +10,6 @@ use std::io; use std::iter; use std::mem; use std::str; -use stringprep; const NONCE_LENGTH: usize = 24; @@ -143,7 +141,8 @@ impl ScramSha256 { v = 0x7e } v as char - }).collect::(); + }) + .collect::(); ScramSha256::new_inner(password, channel_binding, nonce) } @@ -333,7 +332,7 @@ impl<'a> Parser<'a> { fn printable(&mut self) -> io::Result<&'a str> { self.take_while(|c| match c { - '\x21'...'\x2b' | '\x2d'...'\x7e' => true, + '\x21'..='\x2b' | '\x2d'..='\x7e' => true, _ => false, }) } @@ -346,7 +345,7 @@ impl<'a> Parser<'a> { fn base64(&mut self) -> io::Result<&'a str> { self.take_while(|c| match c { - 'a'...'z' | 'A'...'Z' | '0'...'9' | '/' | '+' | '=' => true, + 'a'..='z' | 'A'..='Z' | '0'..='9' | '/' | '+' | '=' => true, _ => false, }) } @@ -359,7 +358,7 @@ impl<'a> Parser<'a> { fn posit_number(&mut self) -> io::Result { let n = self.take_while(|c| match c { - '0'...'9' => true, + '0'..='9' => true, _ => false, })?; n.parse() diff --git a/postgres-protocol/src/lib.rs b/postgres-protocol/src/lib.rs index da06a4c3..aa815c36 100644 --- a/postgres-protocol/src/lib.rs +++ b/postgres-protocol/src/lib.rs @@ -10,18 +10,7 @@ //! This library assumes that the `client_encoding` backend parameter has been //! set to `UTF8`. It will most likely not behave properly if that is not the case. #![doc(html_root_url = "https://docs.rs/postgres-protocol/0.3")] -#![warn(missing_docs)] -extern crate base64; -extern crate byteorder; -extern crate bytes; -extern crate fallible_iterator; -extern crate generic_array; -extern crate hmac; -extern crate md5; -extern crate memchr; -extern crate rand; -extern crate sha2; -extern crate stringprep; +#![warn(missing_docs, rust_2018_idioms)] use byteorder::{BigEndian, ByteOrder}; use std::io; diff --git a/postgres-protocol/src/message/backend.rs b/postgres-protocol/src/message/backend.rs index eacb5da4..c11516d1 100644 --- a/postgres-protocol/src/message/backend.rs +++ b/postgres-protocol/src/message/backend.rs @@ -9,7 +9,7 @@ use std::io::{self, Read}; use std::ops::Range; use std::str; -use Oid; +use crate::Oid; /// An enum representing Postgres backend messages. pub enum Message { diff --git a/postgres-protocol/src/message/frontend.rs b/postgres-protocol/src/message/frontend.rs index a0c20a83..edb929ef 100644 --- a/postgres-protocol/src/message/frontend.rs +++ b/postgres-protocol/src/message/frontend.rs @@ -6,7 +6,7 @@ use std::error::Error; use std::io; use std::marker; -use {write_nullable, FromUsize, IsNull, Oid}; +use crate::{write_nullable, FromUsize, IsNull, Oid}; pub enum Message<'a> { Bind { @@ -148,13 +148,13 @@ where } pub enum BindError { - Conversion(Box), + Conversion(Box), Serialization(io::Error), } -impl From> for BindError { +impl From> for BindError { #[inline] - fn from(e: Box) -> BindError { + fn from(e: Box) -> BindError { BindError::Conversion(e) } } @@ -179,7 +179,7 @@ pub fn bind( where I: IntoIterator, J: IntoIterator, - F: FnMut(T, &mut Vec) -> Result>, + F: FnMut(T, &mut Vec) -> Result>, K: IntoIterator, { buf.push(b'B'); @@ -225,7 +225,8 @@ pub fn cancel_request(process_id: i32, secret_key: i32, buf: &mut Vec) { buf.write_i32::(80877102).unwrap(); buf.write_i32::(process_id).unwrap(); buf.write_i32::(secret_key) - }).unwrap(); + }) + .unwrap(); } #[inline] diff --git a/postgres-protocol/src/types.rs b/postgres-protocol/src/types.rs index 1066ee6a..b5d65cb3 100644 --- a/postgres-protocol/src/types.rs +++ b/postgres-protocol/src/types.rs @@ -5,7 +5,7 @@ use std::boxed::Box as StdBox; use std::error::Error; use std::str; -use {write_nullable, FromUsize, IsNull, Oid}; +use crate::{write_nullable, FromUsize, IsNull, Oid}; const RANGE_UPPER_UNBOUNDED: u8 = 0b0001_0000; const RANGE_LOWER_UNBOUNDED: u8 = 0b0000_1000; @@ -21,7 +21,7 @@ pub fn bool_to_sql(v: bool, buf: &mut Vec) { /// Deserializes a `BOOL` value. #[inline] -pub fn bool_from_sql(buf: &[u8]) -> Result> { +pub fn bool_from_sql(buf: &[u8]) -> Result> { if buf.len() != 1 { return Err("invalid buffer size".into()); } @@ -49,7 +49,7 @@ pub fn text_to_sql(v: &str, buf: &mut Vec) { /// Deserializes a `TEXT`, `VARCHAR`, `CHAR(n)`, `NAME`, or `CITEXT` value. #[inline] -pub fn text_from_sql(buf: &[u8]) -> Result<&str, StdBox> { +pub fn text_from_sql(buf: &[u8]) -> Result<&str, StdBox> { Ok(str::from_utf8(buf)?) } @@ -61,7 +61,7 @@ pub fn char_to_sql(v: i8, buf: &mut Vec) { /// Deserializes a `"char"` value. #[inline] -pub fn char_from_sql(mut buf: &[u8]) -> Result> { +pub fn char_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_i8()?; if !buf.is_empty() { return Err("invalid buffer size".into()); @@ -77,7 +77,7 @@ pub fn int2_to_sql(v: i16, buf: &mut Vec) { /// Deserializes an `INT2` value. #[inline] -pub fn int2_from_sql(mut buf: &[u8]) -> Result> { +pub fn int2_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_i16::()?; if !buf.is_empty() { return Err("invalid buffer size".into()); @@ -93,7 +93,7 @@ pub fn int4_to_sql(v: i32, buf: &mut Vec) { /// Deserializes an `INT4` value. #[inline] -pub fn int4_from_sql(mut buf: &[u8]) -> Result> { +pub fn int4_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_i32::()?; if !buf.is_empty() { return Err("invalid buffer size".into()); @@ -109,7 +109,7 @@ pub fn oid_to_sql(v: Oid, buf: &mut Vec) { /// Deserializes an `OID` value. #[inline] -pub fn oid_from_sql(mut buf: &[u8]) -> Result> { +pub fn oid_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_u32::()?; if !buf.is_empty() { return Err("invalid buffer size".into()); @@ -125,7 +125,7 @@ pub fn int8_to_sql(v: i64, buf: &mut Vec) { /// Deserializes an `INT8` value. #[inline] -pub fn int8_from_sql(mut buf: &[u8]) -> Result> { +pub fn int8_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_i64::()?; if !buf.is_empty() { return Err("invalid buffer size".into()); @@ -141,7 +141,7 @@ pub fn float4_to_sql(v: f32, buf: &mut Vec) { /// Deserializes a `FLOAT4` value. #[inline] -pub fn float4_from_sql(mut buf: &[u8]) -> Result> { +pub fn float4_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_f32::()?; if !buf.is_empty() { return Err("invalid buffer size".into()); @@ -157,7 +157,7 @@ pub fn float8_to_sql(v: f64, buf: &mut Vec) { /// Deserializes a `FLOAT8` value. #[inline] -pub fn float8_from_sql(mut buf: &[u8]) -> Result> { +pub fn float8_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_f64::()?; if !buf.is_empty() { return Err("invalid buffer size".into()); @@ -167,7 +167,7 @@ pub fn float8_from_sql(mut buf: &[u8]) -> Result(values: I, buf: &mut Vec) -> Result<(), StdBox> +pub fn hstore_to_sql<'a, I>(values: I, buf: &mut Vec) -> Result<(), StdBox> where I: IntoIterator)>, { @@ -194,7 +194,7 @@ where Ok(()) } -fn write_pascal_string(s: &str, buf: &mut Vec) -> Result<(), StdBox> { +fn write_pascal_string(s: &str, buf: &mut Vec) -> Result<(), StdBox> { let size = i32::from_usize(s.len())?; buf.write_i32::(size).unwrap(); buf.extend_from_slice(s.as_bytes()); @@ -205,7 +205,7 @@ fn write_pascal_string(s: &str, buf: &mut Vec) -> Result<(), StdBox( mut buf: &'a [u8], -) -> Result, StdBox> { +) -> Result, StdBox> { let count = buf.read_i32::()?; if count < 0 { return Err("invalid entry count".into()); @@ -225,10 +225,10 @@ pub struct HstoreEntries<'a> { impl<'a> FallibleIterator for HstoreEntries<'a> { type Item = (&'a str, Option<&'a str>); - type Error = StdBox; + type Error = StdBox; #[inline] - fn next(&mut self) -> Result)>, StdBox> { + fn next(&mut self) -> Result)>, StdBox> { if self.remaining == 0 { if !self.buf.is_empty() { return Err("invalid buffer size".into()); @@ -272,7 +272,7 @@ pub fn varbit_to_sql( len: usize, v: I, buf: &mut Vec, -) -> Result<(), StdBox> +) -> Result<(), StdBox> where I: Iterator, { @@ -288,7 +288,7 @@ where /// Deserializes a `VARBIT` or `BIT` value. #[inline] -pub fn varbit_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { +pub fn varbit_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { let len = buf.read_i32::()?; if len < 0 { return Err("invalid varbit length".into()); @@ -336,7 +336,7 @@ pub fn timestamp_to_sql(v: i64, buf: &mut Vec) { /// /// The value represents the number of microseconds since midnight, January 1st, 2000. #[inline] -pub fn timestamp_from_sql(mut buf: &[u8]) -> Result> { +pub fn timestamp_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_i64::()?; if !buf.is_empty() { return Err("invalid message length".into()); @@ -356,7 +356,7 @@ pub fn date_to_sql(v: i32, buf: &mut Vec) { /// /// The value represents the number of days since January 1st, 2000. #[inline] -pub fn date_from_sql(mut buf: &[u8]) -> Result> { +pub fn date_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_i32::()?; if !buf.is_empty() { return Err("invalid message length".into()); @@ -376,7 +376,7 @@ pub fn time_to_sql(v: i64, buf: &mut Vec) { /// /// The value represents the number of microseconds since midnight. #[inline] -pub fn time_from_sql(mut buf: &[u8]) -> Result> { +pub fn time_from_sql(mut buf: &[u8]) -> Result> { let v = buf.read_i64::()?; if !buf.is_empty() { return Err("invalid message length".into()); @@ -392,7 +392,7 @@ pub fn macaddr_to_sql(v: [u8; 6], buf: &mut Vec) { /// Deserializes a `MACADDR` value. #[inline] -pub fn macaddr_from_sql(buf: &[u8]) -> Result<[u8; 6], StdBox> { +pub fn macaddr_from_sql(buf: &[u8]) -> Result<[u8; 6], StdBox> { if buf.len() != 6 { return Err("invalid message length".into()); } @@ -409,7 +409,7 @@ pub fn uuid_to_sql(v: [u8; 16], buf: &mut Vec) { /// Deserializes a `UUID` value. #[inline] -pub fn uuid_from_sql(buf: &[u8]) -> Result<[u8; 16], StdBox> { +pub fn uuid_from_sql(buf: &[u8]) -> Result<[u8; 16], StdBox> { if buf.len() != 16 { return Err("invalid message length".into()); } @@ -426,11 +426,11 @@ pub fn array_to_sql( elements: J, mut serializer: F, buf: &mut Vec, -) -> Result<(), StdBox> +) -> Result<(), StdBox> where I: IntoIterator, J: IntoIterator, - F: FnMut(T, &mut Vec) -> Result>, + F: FnMut(T, &mut Vec) -> Result>, { let dimensions_idx = buf.len(); buf.extend_from_slice(&[0; 4]); @@ -469,7 +469,7 @@ where /// Deserializes an array value. #[inline] -pub fn array_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { +pub fn array_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { let dimensions = buf.read_i32::()?; if dimensions < 0 { return Err("invalid dimension count".into()); @@ -547,10 +547,10 @@ pub struct ArrayDimensions<'a>(&'a [u8]); impl<'a> FallibleIterator for ArrayDimensions<'a> { type Item = ArrayDimension; - type Error = StdBox; + type Error = StdBox; #[inline] - fn next(&mut self) -> Result, StdBox> { + fn next(&mut self) -> Result, StdBox> { if self.0.is_empty() { return Ok(None); } @@ -589,10 +589,10 @@ pub struct ArrayValues<'a> { impl<'a> FallibleIterator for ArrayValues<'a> { type Item = Option<&'a [u8]>; - type Error = StdBox; + type Error = StdBox; #[inline] - fn next(&mut self) -> Result>, StdBox> { + fn next(&mut self) -> Result>, StdBox> { if self.remaining == 0 { if !self.buf.is_empty() { return Err("invalid message length".into()); @@ -634,10 +634,10 @@ pub fn range_to_sql( lower: F, upper: G, buf: &mut Vec, -) -> Result<(), StdBox> +) -> Result<(), StdBox> where - F: FnOnce(&mut Vec) -> Result, StdBox>, - G: FnOnce(&mut Vec) -> Result, StdBox>, + F: FnOnce(&mut Vec) -> Result, StdBox>, + G: FnOnce(&mut Vec) -> Result, StdBox>, { let tag_idx = buf.len(); buf.push(0); @@ -663,9 +663,9 @@ where fn write_bound( bound: F, buf: &mut Vec, -) -> Result, StdBox> +) -> Result, StdBox> where - F: FnOnce(&mut Vec) -> Result, StdBox>, + F: FnOnce(&mut Vec) -> Result, StdBox>, { let base = buf.len(); buf.extend_from_slice(&[0; 4]); @@ -702,7 +702,7 @@ pub enum RangeBound { /// Deserializes a range value. #[inline] -pub fn range_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { +pub fn range_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { let tag = buf.read_u8()?; if tag == RANGE_EMPTY { @@ -728,7 +728,7 @@ fn read_bound<'a>( tag: u8, unbounded: u8, inclusive: u8, -) -> Result>, StdBox> { +) -> Result>, StdBox> { if tag & unbounded != 0 { Ok(RangeBound::Unbounded) } else { @@ -770,7 +770,7 @@ pub fn point_to_sql(x: f64, y: f64, buf: &mut Vec) { /// Deserializes a point value. #[inline] -pub fn point_from_sql(mut buf: &[u8]) -> Result> { +pub fn point_from_sql(mut buf: &[u8]) -> Result> { let x = buf.read_f64::()?; let y = buf.read_f64::()?; if !buf.is_empty() { @@ -811,7 +811,7 @@ pub fn box_to_sql(x1: f64, y1: f64, x2: f64, y2: f64, buf: &mut Vec) { /// Deserializes a box value. #[inline] -pub fn box_from_sql(mut buf: &[u8]) -> Result> { +pub fn box_from_sql(mut buf: &[u8]) -> Result> { let x1 = buf.read_f64::()?; let y1 = buf.read_f64::()?; let x2 = buf.read_f64::()?; @@ -852,7 +852,7 @@ pub fn path_to_sql( closed: bool, points: I, buf: &mut Vec, -) -> Result<(), StdBox> +) -> Result<(), StdBox> where I: IntoIterator, { @@ -875,7 +875,7 @@ where /// Deserializes a Postgres path. #[inline] -pub fn path_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { +pub fn path_from_sql<'a>(mut buf: &'a [u8]) -> Result, StdBox> { let closed = buf.read_u8()? != 0; let points = buf.read_i32::()?; @@ -918,10 +918,10 @@ pub struct PathPoints<'a> { impl<'a> FallibleIterator for PathPoints<'a> { type Item = Point; - type Error = StdBox; + type Error = StdBox; #[inline] - fn next(&mut self) -> Result, StdBox> { + fn next(&mut self) -> Result, StdBox> { if self.remaining == 0 { if !self.buf.is_empty() { return Err("invalid message length".into()); @@ -949,7 +949,7 @@ mod test { use std::collections::HashMap; use super::*; - use IsNull; + use crate::IsNull; #[test] fn bool() {