Skip to main content

AtomicOperation

Trait AtomicOperation 

Source
pub trait AtomicOperation: Send {
    // Required method
    fn as_executor(&mut self) -> &mut PgConnection;

    // Provided methods
    fn maybe_now(&self) -> Option<DateTime<Utc>> { ... }
    fn clock(&self) -> &ClockHandle { ... }
    fn add_commit_hook<H: CommitHook>(&mut self, hook: H) -> Result<(), H> { ... }
    fn commit_hook<H: CommitHook>(&self) -> Option<&H> { ... }
}
Expand description

Trait to signify we can make multiple consistent database roundtrips.

Its a stand in for &mut sqlx::Transaction<'_, DB>. The reason for having a trait is to support custom types that wrap the inner transaction while providing additional functionality.

See DbOp or DbOpWithTime.

Required Methods§

Source

fn as_executor(&mut self) -> &mut PgConnection

Returns the sqlx::Executor implementation. The desired way to represent this would actually be as a GAT:

trait AtomicOperation {
    type Executor<'c>: sqlx::PgExecutor<'c>
        where Self: 'c;

    fn as_executor<'c>(&'c mut self) -> Self::Executor<'c>;
}

But GATs don’t play well with async_trait::async_trait due to lifetime constraints so we return the concrete &mut db::Connection instead as a work around.

Since this trait is generally applied to types that wrap a sqlx::Transaction there is no variance in the return type - so its fine.

Provided Methods§

Source

fn maybe_now(&self) -> Option<DateTime<Utc>>

Function for querying when the operation is taking place - if it is cached.

Source

fn clock(&self) -> &ClockHandle

Returns the clock handle for time operations.

Default implementation returns the global clock handle.

Source

fn add_commit_hook<H: CommitHook>(&mut self, hook: H) -> Result<(), H>

Registers a commit hook that will run pre_commit before and post_commit after the transaction commits. Returns Ok(()) if the hook was registered, Err(hook) if hooks are not supported.

Source

fn commit_hook<H: CommitHook>(&self) -> Option<&H>

Typed shared access to the currently-accumulating commit hook of type H, if this operation supports commit hooks and one is registered. Returns the hook a subsequent add_commit_hook::<H> call would merge into.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'c> AtomicOperation for Transaction<'c, Db>

Source§

fn as_executor(&mut self) -> &mut Connection

Implementors§