mysql_async/connection_like/
mod.rs

1// Copyright (c) 2017 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use futures_util::FutureExt;
10
11use crate::{BoxFuture, Pool};
12
13/// Connection.
14#[derive(Debug)]
15pub enum Connection<'a, 't: 'a> {
16    /// Just a connection.
17    Conn(crate::Conn),
18    /// Mutable reference to a connection.
19    ConnMut(&'a mut crate::Conn),
20    /// Connection wrapped in a transaction.
21    Tx(&'a mut crate::Transaction<'t>),
22}
23
24impl From<crate::Conn> for Connection<'static, 'static> {
25    fn from(conn: crate::Conn) -> Self {
26        Connection::Conn(conn)
27    }
28}
29
30impl<'a> From<&'a mut crate::Conn> for Connection<'a, 'static> {
31    fn from(conn: &'a mut crate::Conn) -> Self {
32        Connection::ConnMut(conn)
33    }
34}
35
36impl<'a, 't> From<&'a mut crate::Transaction<'t>> for Connection<'a, 't> {
37    fn from(tx: &'a mut crate::Transaction<'t>) -> Self {
38        Connection::Tx(tx)
39    }
40}
41
42impl std::ops::Deref for Connection<'_, '_> {
43    type Target = crate::Conn;
44
45    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
54impl 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        }
61    }
62}
63
64/// Result of `ToConnection::to_connection` call.
65pub enum ToConnectionResult<'a, 't: 'a> {
66    /// Connection is immediately available.
67    Immediate(Connection<'a, 't>),
68    /// We need some time to get a connection and the operation itself may fail.
69    Mediate(BoxFuture<'a, Connection<'a, 't>>),
70}
71
72pub trait ToConnection<'a, 't: 'a>: Send {
73    fn to_connection(self) -> ToConnectionResult<'a, 't>;
74}
75
76impl<'a, 't: 'a, T: Into<Connection<'a, 't>> + Send> ToConnection<'a, 't> for T {
77    fn to_connection(self) -> ToConnectionResult<'a, 't> {
78        ToConnectionResult::Immediate(self.into())
79    }
80}
81
82impl ToConnection<'static, 'static> for Pool {
83    fn to_connection(self) -> ToConnectionResult<'static, 'static> {
84        let fut = async move {
85            let conn = self.get_conn().await?;
86            Ok(conn.into())
87        }
88        .boxed();
89        ToConnectionResult::Mediate(fut)
90    }
91}
92
93impl<'a> ToConnection<'a, 'static> for &'a Pool {
94    fn to_connection(self) -> ToConnectionResult<'a, 'static> {
95        let fut = async move {
96            let conn = self.get_conn().await?;
97            Ok(conn.into())
98        }
99        .boxed();
100        ToConnectionResult::Mediate(fut)
101    }
102}