tokio-rs / rdbc

Rust DataBase Connectivity (RDBC) :: Common Rust API for database drivers
Apache License 2.0
569 stars 25 forks source link

Proposal: avoid borrow_mut() #20

Closed rumatoest closed 4 years ago

rumatoest commented 4 years ago

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:

let cnx = connect_postgres()?;
let mut session = cnx.start_session()?;
let stmt = conn.prepare("SELECT a FROM b WHERE c = ?")?;
// ...
andygrove commented 4 years ago

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<>> ?

KenSuenobu commented 4 years ago

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?

andygrove commented 4 years ago

This is very related to issue #43

andygrove commented 4 years ago

Fixed in https://github.com/andygrove/rdbc/pull/45