Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,44 @@ impl Client {
Ok(rx.await??)
}

/// Invokes the provided function with a [`rusqlite::Connection`].
///
/// Maps the result error type to a custom error; designed to be
/// used in conjunction with [`query_and_then`](https://docs.rs/rusqlite/latest/rusqlite/struct.CachedStatement.html#method.query_and_then).
pub async fn conn_and_then<F, T, E>(&self, func: F) -> Result<T, E>
where
F: FnOnce(&Connection) -> Result<T, E> + Send + 'static,
T: Send + 'static,
E: From<rusqlite::Error> + From<Error> + Send + 'static,
{
let (tx, rx) = oneshot::channel();
self.conn_tx
.send(Command::Func(Box::new(move |conn| {
_ = tx.send(func(conn));
})))
.map_err(Error::from)?;
rx.await.map_err(Error::from)?
}

/// Invokes the provided function with a mutable [`rusqlite::Connection`].
///
/// Maps the result error type to a custom error; designed to be
/// used in conjunction with [`query_and_then`](https://docs.rs/rusqlite/latest/rusqlite/struct.CachedStatement.html#method.query_and_then).
pub async fn conn_mut_and_then<F, T, E>(&self, func: F) -> Result<T, E>
where
F: FnOnce(&mut Connection) -> Result<T, E> + Send + 'static,
T: Send + 'static,
E: From<rusqlite::Error> + From<Error> + Send + 'static,
{
let (tx, rx) = oneshot::channel();
self.conn_tx
.send(Command::Func(Box::new(move |conn| {
_ = tx.send(func(conn));
})))
.map_err(Error::from)?;
rx.await.map_err(Error::from)?
}

/// Closes the underlying sqlite connection.
///
/// After this method returns, all calls to `self::conn()` or
Expand Down