Migrate postgres-protocol to 2018 edition

This commit is contained in:
Steven Fackler
2018-12-08 17:18:08 -08:00
parent 14571ab029
commit 6a5f22cd5d
7 changed files with 59 additions and 69 deletions

View File

@@ -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

View File

@@ -2,6 +2,7 @@
name = "postgres-protocol"
version = "0.3.2"
authors = ["Steven Fackler <sfackler@gmail.com>"]
edition = "2018"
description = "Low level Postgres protocol APIs"
license = "MIT/Apache-2.0"
repository = "https://github.com/sfackler/rust-postgres-protocol"

View File

@@ -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::<String>();
})
.collect::<String>();
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<u32> {
let n = self.take_while(|c| match c {
'0'...'9' => true,
'0'..='9' => true,
_ => false,
})?;
n.parse()

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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<Error + marker::Sync + Send>),
Conversion(Box<dyn Error + marker::Sync + Send>),
Serialization(io::Error),
}
impl From<Box<Error + marker::Sync + Send>> for BindError {
impl From<Box<dyn Error + marker::Sync + Send>> for BindError {
#[inline]
fn from(e: Box<Error + marker::Sync + Send>) -> BindError {
fn from(e: Box<dyn Error + marker::Sync + Send>) -> BindError {
BindError::Conversion(e)
}
}
@@ -179,7 +179,7 @@ pub fn bind<I, J, F, T, K>(
where
I: IntoIterator<Item = i16>,
J: IntoIterator<Item = T>,
F: FnMut(T, &mut Vec<u8>) -> Result<IsNull, Box<Error + marker::Sync + Send>>,
F: FnMut(T, &mut Vec<u8>) -> Result<IsNull, Box<dyn Error + marker::Sync + Send>>,
K: IntoIterator<Item = i16>,
{
buf.push(b'B');
@@ -225,7 +225,8 @@ pub fn cancel_request(process_id: i32, secret_key: i32, buf: &mut Vec<u8>) {
buf.write_i32::<BigEndian>(80877102).unwrap();
buf.write_i32::<BigEndian>(process_id).unwrap();
buf.write_i32::<BigEndian>(secret_key)
}).unwrap();
})
.unwrap();
}
#[inline]

View File

@@ -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<u8>) {
/// Deserializes a `BOOL` value.
#[inline]
pub fn bool_from_sql(buf: &[u8]) -> Result<bool, StdBox<Error + Sync + Send>> {
pub fn bool_from_sql(buf: &[u8]) -> Result<bool, StdBox<dyn Error + Sync + Send>> {
if buf.len() != 1 {
return Err("invalid buffer size".into());
}
@@ -49,7 +49,7 @@ pub fn text_to_sql(v: &str, buf: &mut Vec<u8>) {
/// Deserializes a `TEXT`, `VARCHAR`, `CHAR(n)`, `NAME`, or `CITEXT` value.
#[inline]
pub fn text_from_sql(buf: &[u8]) -> Result<&str, StdBox<Error + Sync + Send>> {
pub fn text_from_sql(buf: &[u8]) -> Result<&str, StdBox<dyn Error + Sync + Send>> {
Ok(str::from_utf8(buf)?)
}
@@ -61,7 +61,7 @@ pub fn char_to_sql(v: i8, buf: &mut Vec<u8>) {
/// Deserializes a `"char"` value.
#[inline]
pub fn char_from_sql(mut buf: &[u8]) -> Result<i8, StdBox<Error + Sync + Send>> {
pub fn char_from_sql(mut buf: &[u8]) -> Result<i8, StdBox<dyn Error + Sync + Send>> {
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<u8>) {
/// Deserializes an `INT2` value.
#[inline]
pub fn int2_from_sql(mut buf: &[u8]) -> Result<i16, StdBox<Error + Sync + Send>> {
pub fn int2_from_sql(mut buf: &[u8]) -> Result<i16, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_i16::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid buffer size".into());
@@ -93,7 +93,7 @@ pub fn int4_to_sql(v: i32, buf: &mut Vec<u8>) {
/// Deserializes an `INT4` value.
#[inline]
pub fn int4_from_sql(mut buf: &[u8]) -> Result<i32, StdBox<Error + Sync + Send>> {
pub fn int4_from_sql(mut buf: &[u8]) -> Result<i32, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_i32::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid buffer size".into());
@@ -109,7 +109,7 @@ pub fn oid_to_sql(v: Oid, buf: &mut Vec<u8>) {
/// Deserializes an `OID` value.
#[inline]
pub fn oid_from_sql(mut buf: &[u8]) -> Result<Oid, StdBox<Error + Sync + Send>> {
pub fn oid_from_sql(mut buf: &[u8]) -> Result<Oid, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_u32::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid buffer size".into());
@@ -125,7 +125,7 @@ pub fn int8_to_sql(v: i64, buf: &mut Vec<u8>) {
/// Deserializes an `INT8` value.
#[inline]
pub fn int8_from_sql(mut buf: &[u8]) -> Result<i64, StdBox<Error + Sync + Send>> {
pub fn int8_from_sql(mut buf: &[u8]) -> Result<i64, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_i64::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid buffer size".into());
@@ -141,7 +141,7 @@ pub fn float4_to_sql(v: f32, buf: &mut Vec<u8>) {
/// Deserializes a `FLOAT4` value.
#[inline]
pub fn float4_from_sql(mut buf: &[u8]) -> Result<f32, StdBox<Error + Sync + Send>> {
pub fn float4_from_sql(mut buf: &[u8]) -> Result<f32, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_f32::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid buffer size".into());
@@ -157,7 +157,7 @@ pub fn float8_to_sql(v: f64, buf: &mut Vec<u8>) {
/// Deserializes a `FLOAT8` value.
#[inline]
pub fn float8_from_sql(mut buf: &[u8]) -> Result<f64, StdBox<Error + Sync + Send>> {
pub fn float8_from_sql(mut buf: &[u8]) -> Result<f64, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_f64::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid buffer size".into());
@@ -167,7 +167,7 @@ pub fn float8_from_sql(mut buf: &[u8]) -> Result<f64, StdBox<Error + Sync + Send
/// Serializes an `HSTORE` value.
#[inline]
pub fn hstore_to_sql<'a, I>(values: I, buf: &mut Vec<u8>) -> Result<(), StdBox<Error + Sync + Send>>
pub fn hstore_to_sql<'a, I>(values: I, buf: &mut Vec<u8>) -> Result<(), StdBox<dyn Error + Sync + Send>>
where
I: IntoIterator<Item = (&'a str, Option<&'a str>)>,
{
@@ -194,7 +194,7 @@ where
Ok(())
}
fn write_pascal_string(s: &str, buf: &mut Vec<u8>) -> Result<(), StdBox<Error + Sync + Send>> {
fn write_pascal_string(s: &str, buf: &mut Vec<u8>) -> Result<(), StdBox<dyn Error + Sync + Send>> {
let size = i32::from_usize(s.len())?;
buf.write_i32::<BigEndian>(size).unwrap();
buf.extend_from_slice(s.as_bytes());
@@ -205,7 +205,7 @@ fn write_pascal_string(s: &str, buf: &mut Vec<u8>) -> Result<(), StdBox<Error +
#[inline]
pub fn hstore_from_sql<'a>(
mut buf: &'a [u8],
) -> Result<HstoreEntries<'a>, StdBox<Error + Sync + Send>> {
) -> Result<HstoreEntries<'a>, StdBox<dyn Error + Sync + Send>> {
let count = buf.read_i32::<BigEndian>()?;
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<Error + Sync + Send>;
type Error = StdBox<dyn Error + Sync + Send>;
#[inline]
fn next(&mut self) -> Result<Option<(&'a str, Option<&'a str>)>, StdBox<Error + Sync + Send>> {
fn next(&mut self) -> Result<Option<(&'a str, Option<&'a str>)>, StdBox<dyn Error + Sync + Send>> {
if self.remaining == 0 {
if !self.buf.is_empty() {
return Err("invalid buffer size".into());
@@ -272,7 +272,7 @@ pub fn varbit_to_sql<I>(
len: usize,
v: I,
buf: &mut Vec<u8>,
) -> Result<(), StdBox<Error + Sync + Send>>
) -> Result<(), StdBox<dyn Error + Sync + Send>>
where
I: Iterator<Item = u8>,
{
@@ -288,7 +288,7 @@ where
/// Deserializes a `VARBIT` or `BIT` value.
#[inline]
pub fn varbit_from_sql<'a>(mut buf: &'a [u8]) -> Result<Varbit<'a>, StdBox<Error + Sync + Send>> {
pub fn varbit_from_sql<'a>(mut buf: &'a [u8]) -> Result<Varbit<'a>, StdBox<dyn Error + Sync + Send>> {
let len = buf.read_i32::<BigEndian>()?;
if len < 0 {
return Err("invalid varbit length".into());
@@ -336,7 +336,7 @@ pub fn timestamp_to_sql(v: i64, buf: &mut Vec<u8>) {
///
/// The value represents the number of microseconds since midnight, January 1st, 2000.
#[inline]
pub fn timestamp_from_sql(mut buf: &[u8]) -> Result<i64, StdBox<Error + Sync + Send>> {
pub fn timestamp_from_sql(mut buf: &[u8]) -> Result<i64, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_i64::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid message length".into());
@@ -356,7 +356,7 @@ pub fn date_to_sql(v: i32, buf: &mut Vec<u8>) {
///
/// The value represents the number of days since January 1st, 2000.
#[inline]
pub fn date_from_sql(mut buf: &[u8]) -> Result<i32, StdBox<Error + Sync + Send>> {
pub fn date_from_sql(mut buf: &[u8]) -> Result<i32, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_i32::<BigEndian>()?;
if !buf.is_empty() {
return Err("invalid message length".into());
@@ -376,7 +376,7 @@ pub fn time_to_sql(v: i64, buf: &mut Vec<u8>) {
///
/// The value represents the number of microseconds since midnight.
#[inline]
pub fn time_from_sql(mut buf: &[u8]) -> Result<i64, StdBox<Error + Sync + Send>> {
pub fn time_from_sql(mut buf: &[u8]) -> Result<i64, StdBox<dyn Error + Sync + Send>> {
let v = buf.read_i64::<BigEndian>()?;
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<u8>) {
/// Deserializes a `MACADDR` value.
#[inline]
pub fn macaddr_from_sql(buf: &[u8]) -> Result<[u8; 6], StdBox<Error + Sync + Send>> {
pub fn macaddr_from_sql(buf: &[u8]) -> Result<[u8; 6], StdBox<dyn Error + Sync + Send>> {
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<u8>) {
/// Deserializes a `UUID` value.
#[inline]
pub fn uuid_from_sql(buf: &[u8]) -> Result<[u8; 16], StdBox<Error + Sync + Send>> {
pub fn uuid_from_sql(buf: &[u8]) -> Result<[u8; 16], StdBox<dyn Error + Sync + Send>> {
if buf.len() != 16 {
return Err("invalid message length".into());
}
@@ -426,11 +426,11 @@ pub fn array_to_sql<T, I, J, F>(
elements: J,
mut serializer: F,
buf: &mut Vec<u8>,
) -> Result<(), StdBox<Error + Sync + Send>>
) -> Result<(), StdBox<dyn Error + Sync + Send>>
where
I: IntoIterator<Item = ArrayDimension>,
J: IntoIterator<Item = T>,
F: FnMut(T, &mut Vec<u8>) -> Result<IsNull, StdBox<Error + Sync + Send>>,
F: FnMut(T, &mut Vec<u8>) -> Result<IsNull, StdBox<dyn Error + Sync + Send>>,
{
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<Array<'a>, StdBox<Error + Sync + Send>> {
pub fn array_from_sql<'a>(mut buf: &'a [u8]) -> Result<Array<'a>, StdBox<dyn Error + Sync + Send>> {
let dimensions = buf.read_i32::<BigEndian>()?;
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<Error + Sync + Send>;
type Error = StdBox<dyn Error + Sync + Send>;
#[inline]
fn next(&mut self) -> Result<Option<ArrayDimension>, StdBox<Error + Sync + Send>> {
fn next(&mut self) -> Result<Option<ArrayDimension>, StdBox<dyn Error + Sync + Send>> {
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<Error + Sync + Send>;
type Error = StdBox<dyn Error + Sync + Send>;
#[inline]
fn next(&mut self) -> Result<Option<Option<&'a [u8]>>, StdBox<Error + Sync + Send>> {
fn next(&mut self) -> Result<Option<Option<&'a [u8]>>, StdBox<dyn Error + Sync + Send>> {
if self.remaining == 0 {
if !self.buf.is_empty() {
return Err("invalid message length".into());
@@ -634,10 +634,10 @@ pub fn range_to_sql<F, G>(
lower: F,
upper: G,
buf: &mut Vec<u8>,
) -> Result<(), StdBox<Error + Sync + Send>>
) -> Result<(), StdBox<dyn Error + Sync + Send>>
where
F: FnOnce(&mut Vec<u8>) -> Result<RangeBound<IsNull>, StdBox<Error + Sync + Send>>,
G: FnOnce(&mut Vec<u8>) -> Result<RangeBound<IsNull>, StdBox<Error + Sync + Send>>,
F: FnOnce(&mut Vec<u8>) -> Result<RangeBound<IsNull>, StdBox<dyn Error + Sync + Send>>,
G: FnOnce(&mut Vec<u8>) -> Result<RangeBound<IsNull>, StdBox<dyn Error + Sync + Send>>,
{
let tag_idx = buf.len();
buf.push(0);
@@ -663,9 +663,9 @@ where
fn write_bound<F>(
bound: F,
buf: &mut Vec<u8>,
) -> Result<RangeBound<()>, StdBox<Error + Sync + Send>>
) -> Result<RangeBound<()>, StdBox<dyn Error + Sync + Send>>
where
F: FnOnce(&mut Vec<u8>) -> Result<RangeBound<IsNull>, StdBox<Error + Sync + Send>>,
F: FnOnce(&mut Vec<u8>) -> Result<RangeBound<IsNull>, StdBox<dyn Error + Sync + Send>>,
{
let base = buf.len();
buf.extend_from_slice(&[0; 4]);
@@ -702,7 +702,7 @@ pub enum RangeBound<T> {
/// Deserializes a range value.
#[inline]
pub fn range_from_sql<'a>(mut buf: &'a [u8]) -> Result<Range<'a>, StdBox<Error + Sync + Send>> {
pub fn range_from_sql<'a>(mut buf: &'a [u8]) -> Result<Range<'a>, StdBox<dyn Error + Sync + Send>> {
let tag = buf.read_u8()?;
if tag == RANGE_EMPTY {
@@ -728,7 +728,7 @@ fn read_bound<'a>(
tag: u8,
unbounded: u8,
inclusive: u8,
) -> Result<RangeBound<Option<&'a [u8]>>, StdBox<Error + Sync + Send>> {
) -> Result<RangeBound<Option<&'a [u8]>>, StdBox<dyn Error + Sync + Send>> {
if tag & unbounded != 0 {
Ok(RangeBound::Unbounded)
} else {
@@ -770,7 +770,7 @@ pub fn point_to_sql(x: f64, y: f64, buf: &mut Vec<u8>) {
/// Deserializes a point value.
#[inline]
pub fn point_from_sql(mut buf: &[u8]) -> Result<Point, StdBox<Error + Sync + Send>> {
pub fn point_from_sql(mut buf: &[u8]) -> Result<Point, StdBox<dyn Error + Sync + Send>> {
let x = buf.read_f64::<BigEndian>()?;
let y = buf.read_f64::<BigEndian>()?;
if !buf.is_empty() {
@@ -811,7 +811,7 @@ pub fn box_to_sql(x1: f64, y1: f64, x2: f64, y2: f64, buf: &mut Vec<u8>) {
/// Deserializes a box value.
#[inline]
pub fn box_from_sql(mut buf: &[u8]) -> Result<Box, StdBox<Error + Sync + Send>> {
pub fn box_from_sql(mut buf: &[u8]) -> Result<Box, StdBox<dyn Error + Sync + Send>> {
let x1 = buf.read_f64::<BigEndian>()?;
let y1 = buf.read_f64::<BigEndian>()?;
let x2 = buf.read_f64::<BigEndian>()?;
@@ -852,7 +852,7 @@ pub fn path_to_sql<I>(
closed: bool,
points: I,
buf: &mut Vec<u8>,
) -> Result<(), StdBox<Error + Sync + Send>>
) -> Result<(), StdBox<dyn Error + Sync + Send>>
where
I: IntoIterator<Item = (f64, f64)>,
{
@@ -875,7 +875,7 @@ where
/// Deserializes a Postgres path.
#[inline]
pub fn path_from_sql<'a>(mut buf: &'a [u8]) -> Result<Path<'a>, StdBox<Error + Sync + Send>> {
pub fn path_from_sql<'a>(mut buf: &'a [u8]) -> Result<Path<'a>, StdBox<dyn Error + Sync + Send>> {
let closed = buf.read_u8()? != 0;
let points = buf.read_i32::<BigEndian>()?;
@@ -918,10 +918,10 @@ pub struct PathPoints<'a> {
impl<'a> FallibleIterator for PathPoints<'a> {
type Item = Point;
type Error = StdBox<Error + Sync + Send>;
type Error = StdBox<dyn Error + Sync + Send>;
#[inline]
fn next(&mut self) -> Result<Option<Point>, StdBox<Error + Sync + Send>> {
fn next(&mut self) -> Result<Option<Point>, StdBox<dyn Error + Sync + Send>> {
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() {