@@ -10,9 +10,9 @@ use futures_util::FutureExt;
10
10
11
11
use crate :: { BoxFuture , Pool } ;
12
12
13
- /// Connection.
13
+ /// Inner [` Connection`] representation .
14
14
#[ derive( Debug ) ]
15
- pub enum Connection < ' a , ' t : ' a > {
15
+ pub ( crate ) enum ConnectionInner < ' a , ' t : ' a > {
16
16
/// Just a connection.
17
17
Conn ( crate :: Conn ) ,
18
18
/// Mutable reference to a connection.
@@ -21,55 +21,127 @@ pub enum Connection<'a, 't: 'a> {
21
21
Tx ( & ' a mut crate :: Transaction < ' t > ) ,
22
22
}
23
23
24
+ impl std:: ops:: Deref for ConnectionInner < ' _ , ' _ > {
25
+ type Target = crate :: Conn ;
26
+
27
+ fn deref ( & self ) -> & Self :: Target {
28
+ match self {
29
+ ConnectionInner :: Conn ( ref conn) => conn,
30
+ ConnectionInner :: ConnMut ( conn) => conn,
31
+ ConnectionInner :: Tx ( tx) => tx. 0 . deref ( ) ,
32
+ }
33
+ }
34
+ }
35
+
36
+ impl std:: ops:: DerefMut for ConnectionInner < ' _ , ' _ > {
37
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
38
+ match self {
39
+ ConnectionInner :: Conn ( conn) => conn,
40
+ ConnectionInner :: ConnMut ( conn) => conn,
41
+ ConnectionInner :: Tx ( tx) => tx. 0 . inner . deref_mut ( ) ,
42
+ }
43
+ }
44
+ }
45
+
46
+ /// Some connection.
47
+ ///
48
+ /// This could at least be queried.
49
+ #[ derive( Debug ) ]
50
+ pub struct Connection < ' a , ' t : ' a > {
51
+ pub ( crate ) inner : ConnectionInner < ' a , ' t > ,
52
+ }
53
+
54
+ impl Connection < ' _ , ' _ > {
55
+ #[ inline]
56
+ pub ( crate ) fn as_mut ( & mut self ) -> & mut crate :: Conn {
57
+ & mut self . inner
58
+ }
59
+ }
60
+
61
+ impl < ' a , ' t : ' a > Connection < ' a , ' t > {
62
+ /// Borrows a [`Connection`] rather than consuming it.
63
+ ///
64
+ /// This is useful to allow calling [`Query`] methods while still retaining
65
+ /// ownership of the original connection.
66
+ ///
67
+ /// # Examples
68
+ ///
69
+ /// ```no_run
70
+ /// # use mysql_async::Connection;
71
+ /// # use mysql_async::prelude::Query;
72
+ /// async fn connection_by_ref(mut connection: Connection<'_, '_>) {
73
+ /// // Perform some query
74
+ /// "SELECT 1".ignore(connection.by_ref()).await.unwrap();
75
+ /// // Perform another query.
76
+ /// // We can only do this because we used `by_ref` earlier.
77
+ /// "SELECT 2".ignore(connection).await.unwrap();
78
+ /// }
79
+ /// ```
80
+ ///
81
+ /// [`Query`]: crate::prelude::Query
82
+ pub fn by_ref ( & mut self ) -> Connection < ' _ , ' _ > {
83
+ Connection {
84
+ inner : ConnectionInner :: ConnMut ( self . as_mut ( ) ) ,
85
+ }
86
+ }
87
+ }
88
+
24
89
impl From < crate :: Conn > for Connection < ' static , ' static > {
25
90
fn from ( conn : crate :: Conn ) -> Self {
26
- Connection :: Conn ( conn)
91
+ Self {
92
+ inner : ConnectionInner :: Conn ( conn) ,
93
+ }
27
94
}
28
95
}
29
96
30
97
impl < ' a > From < & ' a mut crate :: Conn > for Connection < ' a , ' static > {
31
98
fn from ( conn : & ' a mut crate :: Conn ) -> Self {
32
- Connection :: ConnMut ( conn)
99
+ Self {
100
+ inner : ConnectionInner :: ConnMut ( conn) ,
101
+ }
33
102
}
34
103
}
35
104
36
105
impl < ' a , ' t > From < & ' a mut crate :: Transaction < ' t > > for Connection < ' a , ' t > {
37
106
fn from ( tx : & ' a mut crate :: Transaction < ' t > ) -> Self {
38
- Connection :: Tx ( tx)
107
+ Self {
108
+ inner : ConnectionInner :: Tx ( tx) ,
109
+ }
39
110
}
40
111
}
41
112
42
113
impl std:: ops:: Deref for Connection < ' _ , ' _ > {
43
114
type Target = crate :: Conn ;
44
115
45
116
fn deref ( & self ) -> & Self :: Target {
46
- match self {
47
- Connection :: Conn ( ref conn) => conn,
48
- Connection :: ConnMut ( conn) => conn,
49
- Connection :: Tx ( tx) => tx. 0 . deref ( ) ,
50
- }
51
- }
52
- }
53
-
54
- impl std:: ops:: DerefMut for Connection < ' _ , ' _ > {
55
- fn deref_mut ( & mut self ) -> & mut Self :: Target {
56
- match self {
57
- Connection :: Conn ( conn) => conn,
58
- Connection :: ConnMut ( conn) => conn,
59
- Connection :: Tx ( tx) => tx. 0 . deref_mut ( ) ,
60
- }
117
+ & self . inner
61
118
}
62
119
}
63
120
64
- /// Result of `ToConnection::to_connection` call.
121
+ /// Result of a [ `ToConnection::to_connection`] call.
65
122
pub enum ToConnectionResult < ' a , ' t : ' a > {
66
123
/// Connection is immediately available.
67
124
Immediate ( Connection < ' a , ' t > ) ,
68
125
/// We need some time to get a connection and the operation itself may fail.
69
126
Mediate ( BoxFuture < ' a , Connection < ' a , ' t > > ) ,
70
127
}
71
128
129
+ impl < ' a , ' t : ' a > ToConnectionResult < ' a , ' t > {
130
+ /// Resolves `self` to a connection.
131
+ #[ inline]
132
+ pub async fn resolve ( self ) -> crate :: Result < Connection < ' a , ' t > > {
133
+ match self {
134
+ ToConnectionResult :: Immediate ( immediate) => Ok ( immediate) ,
135
+ ToConnectionResult :: Mediate ( mediate) => mediate. await ,
136
+ }
137
+ }
138
+ }
139
+
140
+ /// Everything that can be given in exchange to a connection.
141
+ ///
142
+ /// Note that you could obtain a `'static` connection by giving away `Conn` or `Pool`.
72
143
pub trait ToConnection < ' a , ' t : ' a > : Send {
144
+ /// Converts self to a connection.
73
145
fn to_connection ( self ) -> ToConnectionResult < ' a , ' t > ;
74
146
}
75
147
0 commit comments