Skip to content

Commit ac591fe

Browse files
committed
complete docs
1 parent fc95cdc commit ac591fe

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ readme = "../README.md"
1010
keywords = ["database", "postgres", "postgresql", "sql", "async"]
1111
categories = ["database"]
1212

13+
[package.metadata.docs.rs]
14+
features = ["docs"]
15+
rustdoc-args = ["--cfg", "feature=\"docs\""]
16+
1317
[badges]
1418
codecov = { repository = "Hexilee/async-postgres" }
1519

@@ -32,6 +36,7 @@ postgres-native-tls = "0.3.0"
3236
native-tls = "0.2.4"
3337

3438
[features]
39+
docs = ["all-types"]
3540
all-types = [
3641
"with-bit-vec-0_6",
3742
"with-chrono-0_4",

README.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,68 @@
1515
</div>
1616
<br>
1717

18-
This crate is an out-of-the-box wrapper of [tokio-postgres](https://crates.io/crates/tokio-postgres).
18+
This crate is a wrapper of [tokio-postgres](https://crates.io/crates/tokio-postgres).
1919

2020
### Pros
2121

22-
- runtime-independent, can be used on any async runtime.
23-
- build-in tls support, based on [tokio-rustls](https://github.com/quininer/tokio-rustls).
22+
Runtime-independent, can be used on any async runtime.
23+
24+
### Usage
25+
26+
Almost the same with tokio-postgres.
27+
28+
- TCP or UDS
29+
30+
```rust
31+
use async_postgres::connect;
32+
use std::error::Error;
33+
use async_std::task::spawn;
34+
35+
async fn play() -> Result<(), Box<dyn Error>> {
36+
let url = "host=localhost user=postgres";
37+
let (client, conn) = connect(url.parse()?).await?;
38+
spawn(conn);
39+
let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
40+
let value: &str = row.get(0);
41+
println!("value: {}", value);
42+
Ok(())
43+
}
44+
```
45+
46+
- TLS
47+
48+
```rust
49+
use async_postgres::connect_tls;
50+
use native_tls::{Certificate, TlsConnector};
51+
use postgres_native_tls::MakeTlsConnector;
52+
use std::fs;
53+
use std::error::Error;
54+
use async_std::task::spawn;
55+
56+
async fn play() -> Result<(), Box<dyn Error>> {
57+
let cert = fs::read("database_cert.pem")?;
58+
let cert = Certificate::from_pem(&cert)?;
59+
let connector = TlsConnector::builder()
60+
.add_root_certificate(cert)
61+
.build()?;
62+
let connector = MakeTlsConnector::new(connector);
63+
let url = "host=localhost user=postgres sslmode=require";
64+
let (client, conn) = connect_tls(url.parse()?, connector).await?;
65+
spawn(conn);
66+
let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
67+
let value: &str = row.get(0);
68+
println!("value: {}", value);
69+
Ok(())
70+
}
71+
```
2472

2573
### Performance
2674

75+
Almost the same with tokio-postgres,
76+
you can see a live benchmark [here](https://github.com/Hexilee/async-postgres/actions?query=workflow%3ABenchmark).
77+
2778
### Develop
79+
80+
Run tests need a running postgres server and environment variables:
81+
- `TCP_URL="postgresql:///<db>?host=<tcp host>&port=<port>&user=<user>&password=<passwd>"`
82+
- `UDS_URL="postgresql:///<db>?host=<postgres uds dir>&port=<port>&user=<user>&password=<passwd>"`

src/connect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async fn connect_socket(
9999
Host::Tcp(tcp) => {
100100
let fut = TcpStream::connect((tcp.as_str(), port));
101101
let socket = timeout(dur, fut).await?;
102-
// socket.set_nodelay(true)?;
102+
socket.set_nodelay(true)?;
103103
Ok(socket.into())
104104
}
105105
}

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
//! A runtime-independent, asynchronous PostgreSQL client.
2-
3-
#![warn(missing_docs)]
1+
#![cfg_attr(feature = "docs", feature(external_doc))]
2+
#![cfg_attr(feature = "docs", doc(include = "../README.md"))]
3+
#![cfg_attr(feature = "docs", warn(missing_docs))]
44

55
#[doc(inline)]
66
pub use connect::connect_tls;
77
#[doc(inline)]
88
pub use socket::Socket;
9+
#[doc(no_inline)]
910
pub use tokio_postgres::*;
1011

1112
use std::io;
@@ -16,7 +17,6 @@ use tokio_postgres::{Client, Connection};
1617
///
1718
/// ```rust
1819
/// use async_postgres::connect;
19-
///
2020
/// use std::error::Error;
2121
/// use async_std::task::spawn;
2222
///

tests/benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn benchmark() -> Result<(), Box<dyn Error>> {
2525
println!(" - tcp: {} us/q", elapsed.as_micros() / queries);
2626
let elapsed = tokio_rr.block_on(tokio_runtime(&uds_url))?;
2727
println!(" - uds: {} us/q", elapsed.as_micros() / queries);
28-
println!(" - tokio_postgres on tokio runtime:");
28+
println!(" - tokio-postgres on tokio runtime:");
2929
let elapsed = tokio_rr.block_on(tokio_postgres(&tcp_url))?;
3030
println!(" - tcp: {} us/q", elapsed.as_micros() / queries);
3131
let elapsed = tokio_rr.block_on(tokio_postgres(&uds_url))?;

0 commit comments

Comments
 (0)