-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
acquire.rs
113 lines (92 loc) · 3.44 KB
/
acquire.rs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::database::Database;
use crate::error::Error;
use crate::pool::{MaybePoolConnection, Pool, PoolConnection};
use crate::transaction::Transaction;
use futures_core::future::BoxFuture;
use std::ops::{Deref, DerefMut};
pub trait Acquire<'c> {
type Database: Database;
type Connection: Deref<Target = <Self::Database as Database>::Connection> + DerefMut;
fn acquire(self) -> BoxFuture<'c, Result<Self::Connection, Error>>;
fn begin(self) -> BoxFuture<'c, Result<Transaction<'c, Self::Database>, Error>>;
}
impl<DB: Database> Acquire<'static> for &'_ Pool<DB> {
type Database = DB;
type Connection = PoolConnection<DB>;
fn acquire(self) -> BoxFuture<'static, Result<Self::Connection, Error>> {
Box::pin(self.acquire())
}
fn begin(self) -> BoxFuture<'static, Result<Transaction<'static, DB>, Error>> {
let conn = self.acquire();
Box::pin(async move {
Transaction::begin(MaybePoolConnection::PoolConnection(conn.await?)).await
})
}
}
#[allow(unused_macros)]
macro_rules! impl_acquire {
($DB:ident, $C:ident) => {
impl<'c> crate::acquire::Acquire<'c> for &'c mut $C {
type Database = $DB;
type Connection = &'c mut <$DB as crate::database::Database>::Connection;
#[inline]
fn acquire(
self,
) -> futures_core::future::BoxFuture<'c, Result<Self::Connection, crate::error::Error>>
{
Box::pin(futures_util::future::ok(self))
}
#[inline]
fn begin(
self,
) -> futures_core::future::BoxFuture<
'c,
Result<crate::transaction::Transaction<'c, $DB>, crate::error::Error>,
> {
crate::transaction::Transaction::begin(self)
}
}
impl<'c> crate::acquire::Acquire<'c> for &'c mut crate::pool::PoolConnection<$DB> {
type Database = $DB;
type Connection = &'c mut <$DB as crate::database::Database>::Connection;
#[inline]
fn acquire(
self,
) -> futures_core::future::BoxFuture<'c, Result<Self::Connection, crate::error::Error>>
{
Box::pin(futures_util::future::ok(&mut **self))
}
#[inline]
fn begin(
self,
) -> futures_core::future::BoxFuture<
'c,
Result<crate::transaction::Transaction<'c, $DB>, crate::error::Error>,
> {
crate::transaction::Transaction::begin(&mut **self)
}
}
impl<'c, 't> crate::acquire::Acquire<'t>
for &'t mut crate::transaction::Transaction<'c, $DB>
{
type Database = $DB;
type Connection = &'t mut <$DB as crate::database::Database>::Connection;
#[inline]
fn acquire(
self,
) -> futures_core::future::BoxFuture<'t, Result<Self::Connection, crate::error::Error>>
{
Box::pin(futures_util::future::ok(&mut **self))
}
#[inline]
fn begin(
self,
) -> futures_core::future::BoxFuture<
't,
Result<crate::transaction::Transaction<'t, $DB>, crate::error::Error>,
> {
crate::transaction::Transaction::begin(&mut **self)
}
}
};
}