tokio_postgres/
to_statement.rs

1use crate::to_statement::private::{Sealed, ToStatementType};
2use crate::Statement;
3
4mod private {
5    use crate::{Client, Error, Statement};
6
7    pub trait Sealed {}
8
9    pub enum ToStatementType<'a> {
10        Statement(&'a Statement),
11        Query(&'a str),
12    }
13
14    impl<'a> ToStatementType<'a> {
15        pub async fn into_statement(self, client: &Client) -> Result<Statement, Error> {
16            match self {
17                ToStatementType::Statement(s) => Ok(s.clone()),
18                ToStatementType::Query(s) => client.prepare(s).await,
19            }
20        }
21    }
22}
23
24/// A trait abstracting over prepared and unprepared statements.
25///
26/// Many methods are generic over this bound, so that they support both a raw query string as well as a statement which
27/// was prepared previously.
28///
29/// This trait is "sealed" and cannot be implemented by anything outside this crate.
30pub trait ToStatement: Sealed {
31    #[doc(hidden)]
32    fn __convert(&self) -> ToStatementType<'_>;
33}
34
35impl ToStatement for Statement {
36    fn __convert(&self) -> ToStatementType<'_> {
37        ToStatementType::Statement(self)
38    }
39}
40
41impl Sealed for Statement {}
42
43impl ToStatement for str {
44    fn __convert(&self) -> ToStatementType<'_> {
45        ToStatementType::Query(self)
46    }
47}
48
49impl Sealed for str {}
50
51impl ToStatement for String {
52    fn __convert(&self) -> ToStatementType<'_> {
53        ToStatementType::Query(self)
54    }
55}
56
57impl Sealed for String {}