diff --git a/postgres-protocol/src/types/test.rs b/postgres-protocol/src/types/test.rs index 7d256355..8796ab31 100644 --- a/postgres-protocol/src/types/test.rs +++ b/postgres-protocol/src/types/test.rs @@ -1,3 +1,4 @@ +use bytes::BytesMut; use fallible_iterator::FallibleIterator; use std::collections::HashMap; @@ -6,32 +7,32 @@ use crate::IsNull; #[test] fn bool() { - let mut buf = vec![]; + let mut buf = BytesMut::new(); bool_to_sql(true, &mut buf); assert_eq!(bool_from_sql(&buf).unwrap(), true); - let mut buf = vec![]; + let mut buf = BytesMut::new(); bool_to_sql(false, &mut buf); assert_eq!(bool_from_sql(&buf).unwrap(), false); } #[test] fn int2() { - let mut buf = vec![]; + let mut buf = BytesMut::new(); int2_to_sql(0x0102, &mut buf); assert_eq!(int2_from_sql(&buf).unwrap(), 0x0102); } #[test] fn int4() { - let mut buf = vec![]; + let mut buf = BytesMut::new(); int4_to_sql(0x0102_0304, &mut buf); assert_eq!(int4_from_sql(&buf).unwrap(), 0x0102_0304); } #[test] fn int8() { - let mut buf = vec![]; + let mut buf = BytesMut::new(); int8_to_sql(0x0102_0304_0506_0708, &mut buf); assert_eq!(int8_from_sql(&buf).unwrap(), 0x0102_0304_0506_0708); } @@ -39,7 +40,7 @@ fn int8() { #[test] #[allow(clippy::float_cmp)] fn float4() { - let mut buf = vec![]; + let mut buf = BytesMut::new(); float4_to_sql(10343.95, &mut buf); assert_eq!(float4_from_sql(&buf).unwrap(), 10343.95); } @@ -47,7 +48,7 @@ fn float4() { #[test] #[allow(clippy::float_cmp)] fn float8() { - let mut buf = vec![]; + let mut buf = BytesMut::new(); float8_to_sql(10343.95, &mut buf); assert_eq!(float8_from_sql(&buf).unwrap(), 10343.95); } @@ -58,7 +59,7 @@ fn hstore() { map.insert("hello", Some("world")); map.insert("hola", None); - let mut buf = vec![]; + let mut buf = BytesMut::new(); hstore_to_sql(map.iter().map(|(&k, &v)| (k, v)), &mut buf).unwrap(); assert_eq!( hstore_from_sql(&buf) @@ -74,7 +75,7 @@ fn varbit() { let len = 12; let bits = [0b0010_1011, 0b0000_1111]; - let mut buf = vec![]; + let mut buf = BytesMut::new(); varbit_to_sql(len, bits.iter().cloned(), &mut buf).unwrap(); let out = varbit_from_sql(&buf).unwrap(); assert_eq!(out.len(), len); @@ -95,7 +96,7 @@ fn array() { ]; let values = [None, Some(&b"hello"[..])]; - let mut buf = vec![]; + let mut buf = BytesMut::new(); array_to_sql( dimensions.iter().cloned(), 10, @@ -132,7 +133,7 @@ fn non_null_array() { ]; let values = [Some(&b"hola"[..]), Some(&b"hello"[..])]; - let mut buf = vec![]; + let mut buf = BytesMut::new(); array_to_sql( dimensions.iter().cloned(), 10, diff --git a/tokio-postgres/src/cancel_query_raw.rs b/tokio-postgres/src/cancel_query_raw.rs index f00d3277..0dcdd8ba 100644 --- a/tokio-postgres/src/cancel_query_raw.rs +++ b/tokio-postgres/src/cancel_query_raw.rs @@ -1,9 +1,9 @@ use crate::config::SslMode; use crate::tls::TlsConnect; use crate::{connect_tls, Error}; +use bytes::BytesMut; use postgres_protocol::message::frontend; use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; -use bytes::BytesMut; pub async fn cancel_query_raw( stream: S, diff --git a/tokio-postgres/src/client.rs b/tokio-postgres/src/client.rs index 78c71e3a..e0133bb3 100644 --- a/tokio-postgres/src/client.rs +++ b/tokio-postgres/src/client.rs @@ -18,7 +18,7 @@ use crate::{cancel_query_raw, copy_in, copy_out, query, Transaction}; use crate::{prepare, SimpleQueryMessage}; use crate::{simple_query, Row}; use crate::{Error, Statement}; -use bytes::{IntoBuf, BytesMut}; +use bytes::{BytesMut, IntoBuf}; use fallible_iterator::FallibleIterator; use futures::channel::mpsc; use futures::{future, TryStream, TryStreamExt}; @@ -118,7 +118,10 @@ impl InnerClient { self.state.lock().types.insert(oid, type_.clone()); } - pub fn with_buf(&self, f: F) -> R where F: FnOnce(&mut BytesMut) -> R { + pub fn with_buf(&self, f: F) -> R + where + F: FnOnce(&mut BytesMut) -> R, + { let mut state = self.state.lock(); let r = f(&mut state.buf); state.buf.clear(); diff --git a/tokio-postgres/src/connect_raw.rs b/tokio-postgres/src/connect_raw.rs index ee04bca6..b96ced03 100644 --- a/tokio-postgres/src/connect_raw.rs +++ b/tokio-postgres/src/connect_raw.rs @@ -4,6 +4,7 @@ use crate::connect_tls::connect_tls; use crate::maybe_tls_stream::MaybeTlsStream; use crate::tls::{ChannelBinding, TlsConnect}; use crate::{Client, Connection, Error}; +use bytes::BytesMut; use fallible_iterator::FallibleIterator; use futures::channel::mpsc; use futures::{ready, Sink, SinkExt, Stream, TryStreamExt}; @@ -18,7 +19,6 @@ use std::pin::Pin; use std::task::{Context, Poll}; use tokio::codec::Framed; use tokio::io::{AsyncRead, AsyncWrite}; -use bytes::BytesMut; pub struct StartupStream { inner: Framed, PostgresCodec>, diff --git a/tokio-postgres/src/connect_tls.rs b/tokio-postgres/src/connect_tls.rs index 9e4ac3ac..d03357b4 100644 --- a/tokio-postgres/src/connect_tls.rs +++ b/tokio-postgres/src/connect_tls.rs @@ -3,9 +3,9 @@ use crate::maybe_tls_stream::MaybeTlsStream; use crate::tls::private::ForcePrivateApi; use crate::tls::{ChannelBinding, TlsConnect}; use crate::Error; +use bytes::BytesMut; use postgres_protocol::message::frontend; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; -use bytes::BytesMut; pub async fn connect_tls( mut stream: S, diff --git a/tokio-postgres/src/prepare.rs b/tokio-postgres/src/prepare.rs index a0d62154..c00e9127 100644 --- a/tokio-postgres/src/prepare.rs +++ b/tokio-postgres/src/prepare.rs @@ -5,6 +5,7 @@ use crate::error::SqlState; use crate::types::{Field, Kind, Oid, Type}; use crate::{query, slice_iter}; use crate::{Column, Error, Statement}; +use bytes::Bytes; use fallible_iterator::FallibleIterator; use futures::TryStreamExt; use pin_utils::pin_mut; @@ -14,7 +15,6 @@ use std::future::Future; use std::pin::Pin; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; -use bytes::Bytes; const TYPEINFO_QUERY: &str = "\ SELECT t.typname, t.typtype, t.typelem, r.rngsubtype, t.typbasetype, n.nspname, t.typrelid diff --git a/tokio-postgres/src/query.rs b/tokio-postgres/src/query.rs index 6d3d198c..cc362eac 100644 --- a/tokio-postgres/src/query.rs +++ b/tokio-postgres/src/query.rs @@ -3,12 +3,12 @@ use crate::codec::FrontendMessage; use crate::connection::RequestMessages; use crate::types::{IsNull, ToSql}; use crate::{Error, Portal, Row, Statement}; +use bytes::{Bytes, BytesMut}; use futures::{ready, Stream}; use postgres_protocol::message::backend::Message; use postgres_protocol::message::frontend; use std::pin::Pin; use std::task::{Context, Poll}; -use bytes::{Bytes, BytesMut}; pub async fn query<'a, I>( client: &InnerClient, @@ -102,7 +102,12 @@ where }) } -pub fn encode_bind<'a, I>(statement: &Statement, params: I, portal: &str, buf: &mut BytesMut) -> Result<(), Error> +pub fn encode_bind<'a, I>( + statement: &Statement, + params: I, + portal: &str, + buf: &mut BytesMut, +) -> Result<(), Error> where I: IntoIterator, I::IntoIter: ExactSizeIterator, diff --git a/tokio-postgres/src/simple_query.rs b/tokio-postgres/src/simple_query.rs index b9d9b65d..11d04a1a 100644 --- a/tokio-postgres/src/simple_query.rs +++ b/tokio-postgres/src/simple_query.rs @@ -2,6 +2,7 @@ use crate::client::{InnerClient, Responses}; use crate::codec::FrontendMessage; use crate::connection::RequestMessages; use crate::{Error, SimpleQueryMessage, SimpleQueryRow}; +use bytes::Bytes; use fallible_iterator::FallibleIterator; use futures::{ready, Stream}; use postgres_protocol::message::backend::Message; @@ -9,7 +10,6 @@ use postgres_protocol::message::frontend; use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; -use bytes::Bytes; pub async fn simple_query(client: &InnerClient, query: &str) -> Result { let buf = encode(client, query)?; diff --git a/tokio-postgres/src/transaction.rs b/tokio-postgres/src/transaction.rs index b70311e4..686cd1ff 100644 --- a/tokio-postgres/src/transaction.rs +++ b/tokio-postgres/src/transaction.rs @@ -14,9 +14,9 @@ use crate::{ use bytes::IntoBuf; use futures::{TryStream, TryStreamExt}; use postgres_protocol::message::frontend; +use postgres_types::private::BytesMut; use std::error; use tokio::io::{AsyncRead, AsyncWrite}; -use postgres_types::private::BytesMut; /// A representation of a PostgreSQL database transaction. ///