pub struct DatabaseAsync<S> { /* private fields */ }Expand description
This is an async wrapper for Database.
You can easily achieve the same thing with tokio::task::spawn_blocking,
but this wrapper is a little bit more efficient while also being runtime agnostic.
Implementations§
Source§impl<S: 'static + Send + Sync + Schema> DatabaseAsync<S>
impl<S: 'static + Send + Sync + Schema> DatabaseAsync<S>
Sourcepub fn new(db: Arc<Database<S>>) -> Self
pub fn new(db: Arc<Database<S>>) -> Self
Create an async wrapper for the Database.
The database is wrapped in an Arc as it needs to be shared with any thread executing a transaction. These threads can live longer than the future that started the transaction.
By accepting an Arc, you can keep your own clone of the Arc and use the database synchronously and asynchronously at the same time!
Sourcepub async fn transaction<R: 'static + Send>(
&self,
f: impl 'static + Send + FnOnce(&'static Transaction<S>) -> R,
) -> R
pub async fn transaction<R: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static Transaction<S>) -> R, ) -> R
This is a lot like Database::transaction, the only difference is that the async function
does not block the runtime and requires the closure to be 'static.
The static requirement is because the future may be canceled, but the transaction can not
be canceled.
Sourcepub async fn transaction_mut<O: 'static + Send, E: 'static + Send>(
&self,
f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> Result<O, E>,
) -> Result<O, E>
pub async fn transaction_mut<O: 'static + Send, E: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> Result<O, E>, ) -> Result<O, E>
This is a lot like Database::transaction_mut, the only difference is that the async function
does not block the runtime and requires the closure to be 'static.
The static requirement is because the future may be canceled, but the transaction can not
be canceled.
Sourcepub async fn transaction_mut_ok<R: 'static + Send>(
&self,
f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> R,
) -> R
pub async fn transaction_mut_ok<R: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> R, ) -> R
This is a lot like Database::transaction_mut_ok, the only difference is that the async function
does not block the runtime and requires the closure to be 'static.
The static requirement is because the future may be canceled, but the transaction can not
be canceled.