DatabaseAsync

Struct DatabaseAsync 

Source
pub struct DatabaseAsync<S> { /* private fields */ }
Expand description

This is an async wrapper for Database.

Transaction methods on this type are identical to Database, but they spawn an async task and return a future to await the transaction completion. Because of this difference, the transaction closures must be 'static.

You can easily implement DatabaseAsync manually using 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>

Source

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!

Source

pub async fn transaction<R: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static Transaction<S>) -> R, ) -> R

Create an immutable Transaction. Immutable transactions never need to wait on any other transactions. The transaction will be executed on a read-only snapshot of the database.

This function will panic if the schema was modified compared to when the Database value was created. This can happen for example by running another instance of your program with additional migrations.

Note that many systems have a limit on the number of file descriptors that can exist in a single process. On my machine the soft limit is (1024) by default. If this limit is reached, it may cause a panic in this method.

Source

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>

Create a mutable Transaction. This operation needs to wait for all other mutable Transactions for this database to be finished. There is currently no timeout on this operation, so it will wait indefinitly if required.

Whether the transaction is commited depends on the result of the closure. The transaction is only commited if the closure return Ok. In the case that it returns Err or when the closure panics, a rollback is performed.

This function will panic if the schema was modified compared to when the Database value was created. This can happen for example by running another instance of your program with additional migrations.

Note that many systems have a limit on the number of file descriptors that can exist in a single process. On my machine the soft limit is (1024) by default. If this limit is reached, it may cause a panic in this method.

Source

pub async fn transaction_mut_ok<R: 'static + Send>( &self, f: impl 'static + Send + FnOnce(&'static mut Transaction<S>) -> R, ) -> R

Same as Self::transaction_mut, but always commits the transaction.

The only exception is that if the closure panics, a rollback is performed.

Trait Implementations§

Source§

impl<S> Clone for DatabaseAsync<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<S> Freeze for DatabaseAsync<S>

§

impl<S> !RefUnwindSafe for DatabaseAsync<S>

§

impl<S> Send for DatabaseAsync<S>
where S: Sync + Send,

§

impl<S> Sync for DatabaseAsync<S>
where S: Sync + Send,

§

impl<S> Unpin for DatabaseAsync<S>

§

impl<S> !UnwindSafe for DatabaseAsync<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V