Skip to content

Commit eb53d51

Browse files
committed
Add a doc example
1 parent def38b4 commit eb53d51

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tokio-postgres/src/lib.rs

+50
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
11
//! An asynchronous Postgres driver using Tokio.
2+
//!
3+
//! # Example
4+
//!
5+
//! ```rust,no_run
6+
//! extern crate futures;
7+
//! extern crate futures_state_stream;
8+
//! extern crate tokio_core;
9+
//! extern crate tokio_postgres;
10+
//!
11+
//! use futures::Future;
12+
//! use futures_state_stream::StateStream;
13+
//! use tokio_core::reactor::Core;
14+
//! use tokio_postgres::{Connection, TlsMode};
15+
//!
16+
//! struct Person {
17+
//! id: i32,
18+
//! name: String,
19+
//! data: Option<Vec<u8>>
20+
//! }
21+
//!
22+
//! fn main() {
23+
//! let mut l = Core::new().unwrap();
24+
//! let done = Connection::connect("postgresql://postgres@localhost", TlsMode::None, &l.handle())
25+
//! .then(|c| {
26+
//! c.unwrap()
27+
//! .batch_execute("CREATE TABLE person (
28+
//! id SERIAL PRIMARY KEY,
29+
//! name VARCHAR NOT NULL,
30+
//! data BYTEA
31+
//! )")
32+
//! })
33+
//! .and_then(|c| c.prepare("INSERT INTO person (name, data) VALUES ($1, $2)"))
34+
//! .and_then(|(s, c)| c.execute(&s, &[&"Steven", &None::<Vec<u8>>]))
35+
//! .and_then(|(_, c)| c.prepare("SELECT id, name, data FROM person"))
36+
//! .and_then(|(s, c)| {
37+
//! c.query(&s, &[])
38+
//! .for_each(|row| {
39+
//! let person = Person {
40+
//! id: row.get(0),
41+
//! name: row.get(1),
42+
//! data: row.get(2),
43+
//! };
44+
//! println!("Found person {}", person.name);
45+
//! Ok(())
46+
//! })
47+
//! });
48+
//!
49+
//! l.run(done).unwrap();
50+
//! }
51+
//! ```
252
#![warn(missing_docs)]
353

454
extern crate fallible_iterator;

0 commit comments

Comments
 (0)