forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_statement.rs
More file actions
59 lines (49 loc) · 1.35 KB
/
Copy pathto_statement.rs
File metadata and controls
59 lines (49 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use tokio_postgres::Error;
use crate::{Client, Statement, Transaction};
mod sealed {
pub trait Sealed {}
}
#[doc(hidden)]
pub trait Prepare {
fn prepare(&mut self, query: &str) -> Result<Statement, Error>;
}
impl Prepare for Client {
fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query)
}
}
impl<'a> Prepare for Transaction<'a> {
fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.prepare(query)
}
}
/// A trait abstracting over prepared and unprepared statements.
///
/// Many methods are generic over this bound, so that they support both a raw query string as well as a statement which
/// was prepared previously.
///
/// This trait is "sealed" and cannot be implemented by anything outside this crate.
pub trait ToStatement: sealed::Sealed {
#[doc(hidden)]
fn __statement<T>(&self, client: &mut T) -> Result<Statement, Error>
where
T: Prepare;
}
impl sealed::Sealed for str {}
impl ToStatement for str {
fn __statement<T>(&self, client: &mut T) -> Result<Statement, Error>
where
T: Prepare,
{
client.prepare(self)
}
}
impl sealed::Sealed for Statement {}
impl ToStatement for Statement {
fn __statement<T>(&self, _: &mut T) -> Result<Statement, Error>
where
T: Prepare,
{
Ok(self.clone())
}
}