0

I am using https://github.com/rusqlite/rusqlite to interface rust with SQLite. I have an object of type Connection. This connection is held behind a RwLock since the threadsafety mode of SQLite I'm using only guarantees inter-process threadsafety. I'm thus modelling the intra-process thread-safety with this lock.

I now want to explicitly close the connection using a wrapper function around my Project class, which owns one such connection:

struct Project {
  db: RwLock<Connection>
}

impl Project {
  pub fn close(mut self) -> Result<(), ProjectError> {
    self.db.write()?.close()?;
  }
}

This however fails as Connection::close has a signature of pub fn close(self) -> ... with the following error:

cannot move out of dereference of `RwLockWriteGuard<'_, Connection>`
...
move occurs because value has type `Connection`, which does not implement the `Copy` trait

I understand the error but do not understand how can I work around it, since what I'm doing is technically sound.

I need the lock, I need to call the close function explicitly (just dropping it doesn't work for me as I need to catch errors during shutdown for backup reasons). I also find rusqlite's decision to consume self sensible and don't want to fork it.

Thanks in advance

Folling
  • 415
  • 3
  • 12
  • 3
    You need to consume the lock with `self.db.into_inner().unwrap().close()?` or similar. – Sven Marnach Jun 23 '23 at 12:55
  • 1
    FWIW "This connection is held behind a RwLock since the threadsafety mode of SQLite I'm using only guarantees inter-process threadsafety." intra-process safety would be a connection per thread. Also [no mode of sqlite actually fully allows sharing connections](https://github.com/rusqlite/rusqlite/issues/342#issuecomment-592624109), which is part of why rusqlite connections are not `Sync` (only `Send`). – Masklinn Jun 23 '23 at 13:23
  • 1
    Incidentally an rwlock is not super useful: since `Connection` is not `Sync`, `RwLock` is not `Sync` either, so there's no way to have multiple readers, hence read is not useful. And `Connection` mostly uses internal mutability so the distinction between `read()` and `write()` does not do much. A straight `Mutex` would work just as well. – Masklinn Jun 23 '23 at 13:23

0 Answers0