rust-postgres/tokio-postgres/src/lib.rs

102 lines
2.5 KiB
Rust
Raw Normal View History

2017-04-23 22:39:07 +00:00
extern crate bytes;
2016-12-21 03:50:44 +00:00
extern crate fallible_iterator;
2018-06-17 04:29:27 +00:00
extern crate futures_cpupool;
2016-12-20 23:42:28 +00:00
extern crate postgres_protocol;
2018-01-10 05:15:35 +00:00
extern crate postgres_shared;
2018-06-17 04:29:27 +00:00
extern crate tokio_codec;
2017-04-23 22:39:07 +00:00
extern crate tokio_io;
2018-06-17 04:29:27 +00:00
extern crate tokio_tcp;
extern crate tokio_timer;
2018-06-18 12:18:04 +00:00
extern crate want;
#[macro_use]
extern crate futures;
2018-06-17 04:29:27 +00:00
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate state_machine_future;
#[cfg(unix)]
2016-12-20 23:42:28 +00:00
extern crate tokio_uds;
2018-06-17 04:29:27 +00:00
use futures::{Async, Future, Poll};
2016-12-20 23:42:28 +00:00
use std::io;
2017-07-20 04:22:27 +00:00
#[doc(inline)]
2018-06-18 12:18:04 +00:00
pub use postgres_shared::stmt::Column;
#[doc(inline)]
pub use postgres_shared::{error, params, types};
2018-04-15 21:38:01 +00:00
#[doc(inline)]
2018-06-17 04:29:27 +00:00
pub use postgres_shared::{CancelData, Notification};
2016-12-25 17:51:12 +00:00
2018-06-17 04:29:27 +00:00
use error::Error;
use params::ConnectParams;
2016-12-20 23:42:28 +00:00
2018-06-17 04:29:27 +00:00
mod proto;
2017-07-20 04:22:27 +00:00
2018-06-17 04:29:27 +00:00
fn bad_response() -> Error {
Error::from(io::Error::new(
io::ErrorKind::InvalidInput,
"the server returned an unexpected response",
))
2016-12-20 23:42:28 +00:00
}
2018-06-17 04:29:27 +00:00
fn disconnected() -> Error {
Error::from(io::Error::new(
io::ErrorKind::UnexpectedEof,
"server disconnected",
))
2016-12-20 23:42:28 +00:00
}
2018-06-18 12:18:04 +00:00
pub struct Client(proto::Client);
2016-12-20 23:42:28 +00:00
2018-06-17 04:29:27 +00:00
impl Client {
pub fn connect(params: ConnectParams) -> Handshake {
Handshake(proto::HandshakeFuture::new(params))
2016-12-20 23:42:28 +00:00
}
2018-06-18 12:18:04 +00:00
/// Polls to to determine whether the connection is ready to send new requests to the backend.
///
/// Requests are unboundedly buffered to enable pipelining, but this risks unbounded memory consumption if requests
/// are produced at a faster pace than the backend can process. This method can be used to cooperatively "throttle"
/// request creation. Specifically, it returns ready when the connection has sent any queued requests and is waiting
/// on new requests from the client.
pub fn poll_ready(&mut self) -> Poll<(), Error> {
self.0.poll_ready()
}
2016-12-20 23:42:28 +00:00
}
2018-06-17 04:29:27 +00:00
pub struct Connection(proto::Connection);
2016-12-21 03:50:44 +00:00
2016-12-20 23:42:28 +00:00
impl Connection {
pub fn cancel_data(&self) -> CancelData {
2018-06-17 04:29:27 +00:00
self.0.cancel_data()
2016-12-20 23:42:28 +00:00
}
2016-12-26 21:21:20 +00:00
2018-06-17 04:29:27 +00:00
pub fn parameter(&self, name: &str) -> Option<&str> {
self.0.parameter(name)
}
2016-12-20 23:42:28 +00:00
}
2018-06-17 04:29:27 +00:00
impl Future for Connection {
type Item = ();
type Error = Error;
2018-06-17 04:29:27 +00:00
fn poll(&mut self) -> Poll<(), Error> {
self.0.poll()
2016-12-21 03:50:44 +00:00
}
}
2018-06-17 04:29:27 +00:00
pub struct Handshake(proto::HandshakeFuture);
2017-09-30 21:56:15 +00:00
2018-06-17 04:29:27 +00:00
impl Future for Handshake {
type Item = (Client, Connection);
type Error = Error;
2017-09-30 21:56:15 +00:00
2018-06-17 04:29:27 +00:00
fn poll(&mut self) -> Poll<(Client, Connection), Error> {
2018-06-18 12:18:04 +00:00
let (client, connection) = try_ready!(self.0.poll());
2017-09-30 21:56:15 +00:00
2018-06-18 12:18:04 +00:00
Ok(Async::Ready((Client(client), Connection(connection))))
2017-09-30 21:56:15 +00:00
}
}