Skip to content

Commit 07c7ffd

Browse files
committed
Finish docs for tokio-postgres
1 parent d35139d commit 07c7ffd

File tree

7 files changed

+80
-23
lines changed

7 files changed

+80
-23
lines changed

tokio-postgres/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub enum SslMode {
4444
Prefer,
4545
/// Require the use of TLS.
4646
Require,
47+
#[doc(hidden)]
48+
__NonExhaustive,
4749
}
4850

4951
#[derive(Debug, Clone, PartialEq)]

tokio-postgres/src/impls.rs

+20
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,23 @@ impl Stream for SimpleQuery {
213213
self.0.poll()
214214
}
215215
}
216+
217+
/// The future returned by `TransactionBuilder::build`.
218+
#[must_use = "futures do nothing unless polled"]
219+
pub struct Transaction<T>(pub(crate) proto::TransactionFuture<T, T::Item, T::Error>)
220+
where
221+
T: Future,
222+
T::Error: From<Error>;
223+
224+
impl<T> Future for Transaction<T>
225+
where
226+
T: Future,
227+
T::Error: From<Error>,
228+
{
229+
type Item = T::Item;
230+
type Error = T::Error;
231+
232+
fn poll(&mut self) -> Poll<T::Item, T::Error> {
233+
self.0.poll()
234+
}
235+
}

tokio-postgres/src/lib.rs

+7-23
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@
8787
//! use futures::Future;
8888
//! use tokio_postgres::{Client, Error, Statement};
8989
//!
90-
//! fn pipelined_prepare(client: &mut Client) -> impl Future<Item = (Statement, Statement), Error = Error>
90+
//! fn pipelined_prepare(
91+
//! client: &mut Client,
92+
//! ) -> impl Future<Item = (Statement, Statement), Error = Error>
9193
//! {
9294
//! client.prepare("SELECT * FROM foo")
9395
//! .join(client.prepare("INSERT INTO bar (id, name) VALUES ($1, $2)"))
@@ -99,7 +101,7 @@
99101
//! The client works with arbitrary `AsyncRead + AsyncWrite` streams. Convenience APIs are provided to handle the
100102
//! connection process, but these are gated by the `runtime` Cargo feature, which is enabled by default. If disabled,
101103
//! all dependence on the tokio runtime is removed.
102-
#![warn(rust_2018_idioms, clippy::all)]
104+
#![warn(rust_2018_idioms, clippy::all, missing_docs)]
103105

104106
use bytes::IntoBuf;
105107
use futures::{Future, Poll, Stream};
@@ -406,32 +408,14 @@ pub struct Portal(proto::Portal);
406408
pub struct TransactionBuilder(proto::Client);
407409

408410
impl TransactionBuilder {
409-
pub fn build<T>(self, future: T) -> Transaction<T>
411+
/// Returns a future which wraps another in a database transaction.
412+
pub fn build<T>(self, future: T) -> impls::Transaction<T>
410413
where
411414
T: Future,
412415
// FIXME error type?
413416
T::Error: From<Error>,
414417
{
415-
Transaction(proto::TransactionFuture::new(self.0, future))
416-
}
417-
}
418-
419-
#[must_use = "futures do nothing unless polled"]
420-
pub struct Transaction<T>(proto::TransactionFuture<T, T::Item, T::Error>)
421-
where
422-
T: Future,
423-
T::Error: From<Error>;
424-
425-
impl<T> Future for Transaction<T>
426-
where
427-
T: Future,
428-
T::Error: From<Error>,
429-
{
430-
type Item = T::Item;
431-
type Error = T::Error;
432-
433-
fn poll(&mut self) -> Poll<T::Item, T::Error> {
434-
self.0.poll()
418+
impls::Transaction(proto::TransactionFuture::new(self.0, future))
435419
}
436420
}
437421

tokio-postgres/src/proto/tls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ where
6363
tls: state.tls,
6464
})
6565
}
66+
SslMode::__NonExhaustive => unreachable!(),
6667
}
6768
}
6869

tokio-postgres/src/row.rs

+23
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ where
9393
}
9494
}
9595

96+
/// A row of data returned from the database by a query.
9697
pub struct Row {
9798
statement: proto::Statement,
9899
body: DataRowBody,
@@ -110,18 +111,28 @@ impl Row {
110111
})
111112
}
112113

114+
/// Returns information about the columns of data in the row.
113115
pub fn columns(&self) -> &[Column] {
114116
self.statement.columns()
115117
}
116118

119+
/// Determines if the row contains no values.
117120
pub fn is_empty(&self) -> bool {
118121
self.len() == 0
119122
}
120123

124+
/// Returns the number of values in the row.
121125
pub fn len(&self) -> usize {
122126
self.columns().len()
123127
}
124128

129+
/// Deserializes a value from the row.
130+
///
131+
/// The value can be specified either by its numeric index in the row, or by its column name.
132+
///
133+
/// # Panics
134+
///
135+
/// Panics if the index is out of bounds or if the value cannot be converted to the specified type.
125136
pub fn get<'a, I, T>(&'a self, idx: I) -> T
126137
where
127138
I: RowIndex + fmt::Display,
@@ -133,6 +144,7 @@ impl Row {
133144
}
134145
}
135146

147+
/// Like `Row::get`, but returns a `Result` rather than panicking.
136148
pub fn try_get<'a, I, T>(&'a self, idx: I) -> Result<T, Error>
137149
where
138150
I: RowIndex,
@@ -161,6 +173,7 @@ impl Row {
161173
}
162174
}
163175

176+
/// A row of data returned from the database by a simple query.
164177
pub struct SimpleQueryRow {
165178
columns: Arc<[String]>,
166179
body: DataRowBody,
@@ -178,14 +191,23 @@ impl SimpleQueryRow {
178191
})
179192
}
180193

194+
/// Determines if the row contains no values.
181195
pub fn is_empty(&self) -> bool {
182196
self.len() == 0
183197
}
184198

199+
/// Returns the number of values in the row.
185200
pub fn len(&self) -> usize {
186201
self.columns.len()
187202
}
188203

204+
/// Returns a value from the row.
205+
///
206+
/// The value can be specified either by its numeric index in the row, or by its column name.
207+
///
208+
/// # Panics
209+
///
210+
/// Panics if the index is out of bounds or if the value cannot be converted to the specified type.
189211
pub fn get<I>(&self, idx: I) -> Option<&str>
190212
where
191213
I: RowIndex + fmt::Display,
@@ -196,6 +218,7 @@ impl SimpleQueryRow {
196218
}
197219
}
198220

221+
/// Like `SimpleQueryRow::get`, but returns a `Result` rather than panicking.
199222
pub fn try_get<I>(&self, idx: I) -> Result<Option<&str>, Error>
200223
where
201224
I: RowIndex,

tokio-postgres/src/socket.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ enum Inner {
1313
Unix(UnixStream),
1414
}
1515

16+
/// The standard stream type used by the crate.
17+
///
18+
/// Requires the `runtime` Cargo feature (enabled by default).
1619
#[derive(Debug)]
1720
pub struct Socket(Inner);
1821

tokio-postgres/src/tls.rs

+24
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,55 @@ pub(crate) mod private {
99
pub struct ForcePrivateApi;
1010
}
1111

12+
/// Channel binding information returned from a TLS handshake.
1213
pub struct ChannelBinding {
1314
pub(crate) tls_server_end_point: Option<Vec<u8>>,
1415
}
1516

1617
impl ChannelBinding {
18+
/// Creates a `ChannelBinding` containing no information.
1719
pub fn none() -> ChannelBinding {
1820
ChannelBinding {
1921
tls_server_end_point: None,
2022
}
2123
}
2224

25+
/// Creates a `ChannelBinding` containing `tls-server-end-point` channel binding information.
2326
pub fn tls_server_end_point(tls_server_end_point: Vec<u8>) -> ChannelBinding {
2427
ChannelBinding {
2528
tls_server_end_point: Some(tls_server_end_point),
2629
}
2730
}
2831
}
2932

33+
/// A constructor of `TlsConnect`ors.
34+
///
35+
/// Requires the `runtime` Cargo feature (enabled by default).
3036
#[cfg(feature = "runtime")]
3137
pub trait MakeTlsConnect<S> {
38+
/// The stream type created by the `TlsConnect` implementation.
3239
type Stream: AsyncRead + AsyncWrite;
40+
/// The `TlsConnect` implementation created by this type.
3341
type TlsConnect: TlsConnect<S, Stream = Self::Stream>;
42+
/// The error type retured by the `TlsConnect` implementation.
3443
type Error: Into<Box<dyn Error + Sync + Send>>;
3544

45+
/// Creates a new `TlsConnect`or.
46+
///
47+
/// The domain name is provided for certificate verification and SNI.
3648
fn make_tls_connect(&mut self, domain: &str) -> Result<Self::TlsConnect, Self::Error>;
3749
}
3850

51+
/// An asynchronous function wrapping a stream in a TLS session.
3952
pub trait TlsConnect<S> {
53+
/// The stream returned by the future.
4054
type Stream: AsyncRead + AsyncWrite;
55+
/// The error type returned by the future.
4156
type Error: Into<Box<dyn Error + Sync + Send>>;
57+
/// The future returned by the connector.
4258
type Future: Future<Item = (Self::Stream, ChannelBinding), Error = Self::Error>;
4359

60+
/// Returns a future performing a TLS handshake over the stream.
4461
fn connect(self, stream: S) -> Self::Future;
4562

4663
#[doc(hidden)]
@@ -49,6 +66,9 @@ pub trait TlsConnect<S> {
4966
}
5067
}
5168

69+
/// A `MakeTlsConnect` and `TlsConnect` implementation which simply returns an error.
70+
///
71+
/// This can be used when `sslmode` is `none` or `prefer`.
5272
#[derive(Debug, Copy, Clone)]
5373
pub struct NoTls;
5474

@@ -77,6 +97,9 @@ impl<S> TlsConnect<S> for NoTls {
7797
}
7898
}
7999

100+
/// The TLS "stream" type produced by the `NoTls` connector.
101+
///
102+
/// Since `NoTls` doesn't support TLS, this type is uninhabited.
80103
pub enum NoTlsStream {}
81104

82105
impl Read for NoTlsStream {
@@ -103,6 +126,7 @@ impl AsyncWrite for NoTlsStream {
103126
}
104127
}
105128

129+
/// The error returned by `NoTls`.
106130
#[derive(Debug)]
107131
pub struct NoTlsError(());
108132

0 commit comments

Comments
 (0)