diesel/pg/serialize/
write_tuple.rs

1use std::io::Write;
2
3use pg::Pg;
4use serialize::{self, Output};
5
6/// Helper trait for writing tuples as named composite types
7///
8/// This trait is essentially `ToSql<Record<ST>>` for tuples.
9/// While we can provide a valid body of `to_sql`,
10/// PostgreSQL doesn't allow the use of bind parameters for unnamed composite types.
11/// For this reason, we avoid implementing `ToSql` directly.
12///
13/// This trait can be used by `ToSql` impls of named composite types.
14///
15/// # Example
16///
17/// ```no_run
18/// # #[macro_use]
19/// # extern crate diesel;
20/// #
21/// # #[cfg(feature = "postgres")]
22/// # mod the_impl {
23/// #     use diesel::pg::Pg;
24/// #     use diesel::serialize::{self, ToSql, Output, WriteTuple};
25/// #     use diesel::sql_types::{Integer, Text};
26/// #     use std::io::Write;
27/// #
28///     #[derive(SqlType)]
29///     #[postgres(type_name = "my_type")]
30///     struct MyType;
31///
32///     #[derive(Debug)]
33///     struct MyStruct<'a>(i32, &'a str);
34///
35///     impl<'a> ToSql<MyType, Pg> for MyStruct<'a> {
36///         fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
37///             WriteTuple::<(Integer, Text)>::write_tuple(
38///                 &(self.0, self.1),
39///                 out,
40///             )
41///         }
42///     }
43/// # }
44/// # fn main() {}
45/// ```
46pub trait WriteTuple<ST> {
47    /// See trait documentation.
48    fn write_tuple<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result;
49}