@@ -10,9 +10,9 @@ use futures_util::FutureExt;
1010
1111use crate :: { BoxFuture , Pool } ;
1212
13- /// Connection.
13+ /// Inner [` Connection`] representation .
1414#[ derive( Debug ) ]
15- pub enum Connection < ' a , ' t : ' a > {
15+ pub ( crate ) enum ConnectionInner < ' a , ' t : ' a > {
1616 /// Just a connection.
1717 Conn ( crate :: Conn ) ,
1818 /// Mutable reference to a connection.
@@ -21,55 +21,127 @@ pub enum Connection<'a, 't: 'a> {
2121 Tx ( & ' a mut crate :: Transaction < ' t > ) ,
2222}
2323
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+
2489impl From < crate :: Conn > for Connection < ' static , ' static > {
2590 fn from ( conn : crate :: Conn ) -> Self {
26- Connection :: Conn ( conn)
91+ Self {
92+ inner : ConnectionInner :: Conn ( conn) ,
93+ }
2794 }
2895}
2996
3097impl < ' a > From < & ' a mut crate :: Conn > for Connection < ' a , ' static > {
3198 fn from ( conn : & ' a mut crate :: Conn ) -> Self {
32- Connection :: ConnMut ( conn)
99+ Self {
100+ inner : ConnectionInner :: ConnMut ( conn) ,
101+ }
33102 }
34103}
35104
36105impl < ' a , ' t > From < & ' a mut crate :: Transaction < ' t > > for Connection < ' a , ' t > {
37106 fn from ( tx : & ' a mut crate :: Transaction < ' t > ) -> Self {
38- Connection :: Tx ( tx)
107+ Self {
108+ inner : ConnectionInner :: Tx ( tx) ,
109+ }
39110 }
40111}
41112
42113impl std:: ops:: Deref for Connection < ' _ , ' _ > {
43114 type Target = crate :: Conn ;
44115
45116 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
61118 }
62119}
63120
64- /// Result of `ToConnection::to_connection` call.
121+ /// Result of a [ `ToConnection::to_connection`] call.
65122pub enum ToConnectionResult < ' a , ' t : ' a > {
66123 /// Connection is immediately available.
67124 Immediate ( Connection < ' a , ' t > ) ,
68125 /// We need some time to get a connection and the operation itself may fail.
69126 Mediate ( BoxFuture < ' a , Connection < ' a , ' t > > ) ,
70127}
71128
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`.
72143pub trait ToConnection < ' a , ' t : ' a > : Send {
144+ /// Converts self to a connection.
73145 fn to_connection ( self ) -> ToConnectionResult < ' a , ' t > ;
74146}
75147
0 commit comments