tokio-rs / rdbc

Rust DataBase Connectivity (RDBC) :: Common Rust API for database drivers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal: avoid borrow_mut()

rumatoest opened this issue · comments

IMHO borrow_mut() does not looks good in terms of rust.

let conn = connect_postgres()?;
let mut conn = conn.borrow_mut();
let stmt = conn.prepare("SELECT a FROM b WHERE c = ?")?;
let mut stmt = stmt.borrow_mut();

What if there will be another approach like:

  • Having generic trait for implementing single connection or connection pool
  • Connection (or pool) is immutable.
  • It can produce sessions
  • Session is used to execute one ore more queries (you can think of session as a transaction)
  • When session goes out of scope it is closed or reused for next calls
let cnx = connect_postgres()?;
let mut session = cnx.start_session()?;
let stmt = conn.prepare("SELECT a FROM b WHERE c = ?")?;
// ...

I was thinking about this today too. The API is very clunky right now. I'm lacking expertise still in certain areas of Rust to know how to fix this.

Should I be returning mutable references rather than using Rc<RefCell<>> ?

well, stmt.borrow() will still work, as long as you have a mutable accessor to handle setters for bound statements. If all you're using is getters, the single borrow should be good. Not necessarily sure that an Rc<> is needed with a RefCell in this case?

This is very related to issue #43

Fixed in #45