I try the next code work. What i doing wrong?.
use std::cell::RefCell;
use std::rc::Rc;
use sqlx::{Acquire, Error, MySql, Transaction};
type DBConn = Rc<RefCell<sqlx::pool::PoolConnection<MySql>>>;
type DBTrans<'t> = Rc<RefCell<sqlx::Transaction<'t, MySql>>>;
// This work!!!. But is only the db connection
async fn connection_mysql<'c>(connection_pool: Option<&'c sqlx::Pool<sqlx::MySql>>) -> Result<DBConn, sqlx::Error> {
match connection_pool {
Some(connection_pool) => {
let conn: sqlx::pool::PoolConnection<sqlx::MySql> = match connection_pool.acquire().await {
Ok(conn) => conn,
Err(error) => return Err(error),
};
let rc_conn: Rc<RefCell<sqlx::pool::PoolConnection<MySql>>> = Rc::new( RefCell::new( conn ) );
Ok(rc_conn)
}
None => {
Err(Error::PoolClosed)
}
}
}
//Not work. I Need the connection and the transaction
async fn begin_transaction_mysql<'c>(connection_pool: Option<&'c sqlx::Pool<sqlx::MySql>>) -> Result<(DBConn,DBTrans), sqlx::Error> {
match connection_pool {
Some(connection_pool) => {
let conn: sqlx::pool::PoolConnection<sqlx::MySql> = match connection_pool.acquire().await {
Ok(conn) => conn,
Err(error) => return Err(error),
};
let rc_conn: Rc<RefCell<sqlx::pool::PoolConnection<MySql>>> = Rc::new( RefCell::new( conn ) );
let mut trans = rc_conn.borrow_mut(); //I try with rc_conn.clone().borrow_mut(). Not work
let rc_transaction: Rc<RefCell<Transaction<'_, MySql>>> = Rc::new( RefCell::new( trans.begin().await.unwrap()) );
Ok(( rc_conn, rc_transaction)) //<-- Problem where
}
None => {
Err(Error::PoolClosed)
}
}
}
The borrow checker has complaints. What i need to do for this work?
Compiling sqlx_mysql_tokio_01 v0.1.0 (/home/dsystems01/Desktop/rust-lang/projects/test/sqlx_mysql_tokio_01)
error[E0515]: cannot return value referencing local variable `trans`
--> src/main.rs:330:13
|
328 | let rc_transaction: Rc<RefCell<Transaction<'_, MySql>>> = Rc::new( RefCell::new( trans.begin().await.unwrap()) );
| ----- `trans` is borrowed here
329 |
330 | Ok(( rc_conn, rc_transaction))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
error[E0515]: cannot return value referencing local variable `rc_conn`
--> src/main.rs:330:13
|
326 | let mut trans = rc_conn.as_ref().borrow_mut(); //.into_inner();
| ------- `rc_conn` is borrowed here
...
330 | Ok(( rc_conn, rc_transaction))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function
error[E0505]: cannot move out of `rc_conn` because it is borrowed
--> src/main.rs:330:18
|
315 | async fn begin_transaction_mysql_01<'c>(connection_pool: Option<&'c sqlx::Pool<sqlx::MySql>>) -> Result<(DBConn,DBTrans), sqlx::Error> {
| -- lifetime `'c` defined here
...
324 | let rc_conn: Rc<RefCell<sqlx::pool::PoolConnection<MySql>>> = Rc::new( RefCell::new( conn ) );
| ------- binding `rc_conn` declared here
325 |
326 | let mut trans = rc_conn.as_ref().borrow_mut(); //.into_inner();
| ------- borrow of `rc_conn` occurs here
...
330 | Ok(( rc_conn, rc_transaction))
| -----^^^^^^^------------------
| | |
| | move out of `rc_conn` occurs here
| returning this value requires that `rc_conn` is borrowed for `'c`
Some errors have detailed explanations: E0505, E0515.
For more information about an error, try `rustc --explain E0505`.
The message "returning this value requires that rc_conn
is borrowed for 'c
" what it mean?
Thanks for all help can get me.