Skip to content

Commit e99d65e

Browse files
committed
Derive documentation
1 parent 01cc7e4 commit e99d65e

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

postgres-types/src/lib.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,109 @@
22
//!
33
//! This crate is used by the `tokio-postgres` and `postgres` crates. You normally don't need to depend directly on it
44
//! unless you want to define your own `ToSql` or `FromSql` definitions.
5+
//!
6+
//! # Derive
7+
//!
8+
//! If the `derive` cargo feature is enabled, you can derive `ToSql` and `FromSql` implementations for custom Postgres
9+
//! types.
10+
//!
11+
//! ## Enums
12+
//!
13+
//! Postgres enums correspond to C-like enums in Rust:
14+
//!
15+
//! ```sql
16+
//! CREATE TYPE "Mood" AS ENUM (
17+
//! 'Sad',
18+
//! 'Ok',
19+
//! 'Happy'
20+
//! );
21+
//! ```
22+
//!
23+
//! ```rust
24+
//! # #[cfg(feature = "derive")]
25+
//! use postgres_types::{ToSql, FromSql};
26+
//!
27+
//! # #[cfg(feature = "derive")]
28+
//! #[derive(Debug, ToSql, FromSql)]
29+
//! enum Mood {
30+
//! Sad,
31+
//! Ok,
32+
//! Happy,
33+
//! }
34+
//! ```
35+
//!
36+
//! ## Domains
37+
//!
38+
//! Postgres domains correspond to tuple structs with one member in Rust:
39+
//!
40+
//! ```sql
41+
//! CREATE DOMAIN "SessionId" AS BYTEA CHECK(octet_length(VALUE) = 16);
42+
//! ```
43+
//!
44+
//! ```rust
45+
//! # #[cfg(feature = "derive")]
46+
//! use postgres_types::{ToSql, FromSql};
47+
//!
48+
//! # #[cfg(feature = "derive")]
49+
//! #[derive(Debug, ToSql, FromSql)]
50+
//! struct SessionId(Vec<u8>);
51+
//! ```
52+
//!
53+
//! ## Composites
54+
//!
55+
//! Postgres composite types correspond to structs in Rust:
56+
//!
57+
//! ```sql
58+
//! CREATE TYPE "InventoryItem" AS (
59+
//! name TEXT,
60+
//! supplier_id INT,
61+
//! price DOUBLE PRECISION
62+
//! );
63+
//! ```
64+
//!
65+
//! ```rust
66+
//! # #[cfg(feature = "derive")]
67+
//! use postgres_types::{ToSql, FromSql};
68+
//!
69+
//! # #[cfg(feature = "derive")]
70+
//! #[derive(Debug, ToSql, FromSql)]
71+
//! struct InventoryItem {
72+
//! name: String,
73+
//! supplier_id: i32,
74+
//! price: Option<f64>,
75+
//! }
76+
//! ```
77+
//!
78+
//! ## Naming
79+
//!
80+
//! The derived implementations will enforce exact matches of type, field, and variant names between the Rust and
81+
//! Postgres types. The `#[postgres(name = "...")]` attribute can be used to adjust the name on a type, variant, or
82+
//! field:
83+
//!
84+
//! ```sql
85+
//! CREATE TYPE mood AS ENUM (
86+
//! 'sad',
87+
//! 'ok',
88+
//! 'happy'
89+
//! );
90+
//! ```
91+
//!
92+
//! ```rust
93+
//! # #[cfg(feature = "derive")]
94+
//! use postgres_types::{ToSql, FromSql};
95+
//!
96+
//! # #[cfg(feature = "derive")]
97+
//! #[derive(Debug, ToSql, FromSql)]
98+
//! #[postgres(name = "mood")]
99+
//! enum Mood {
100+
//! #[postgres(name = "sad")]
101+
//! Sad,
102+
//! #[postgres(name = "ok")]
103+
//! Ok,
104+
//! #[postgres(name = "happy")]
105+
//! Happy,
106+
//! }
107+
//! ```
5108
#![warn(missing_docs)]
6109

7110
use fallible_iterator::FallibleIterator;

0 commit comments

Comments
 (0)