diesel/pg/serialize/
write_tuple.rs

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