Skip to main content

WrapsOperation

Trait WrapsOperation 

Source
pub trait WrapsOperation: Send {
    type Inner: AtomicOperation + ?Sized;

    // Required methods
    fn op(&self) -> &Self::Inner;
    fn op_mut(&mut self) -> &mut Self::Inner;
}
Expand description

Derives the whole of AtomicOperation for a type that wraps another operation, by delegation.

A wrapper that restricts or re-presents an operation — obix’s FlushOp, a newtype that seals off commit(), anything holding a &mut DbOp — needs every AtomicOperation method to forward to the operation inside. Written by hand that is eight near-identical bodies, it must be redone for each new wrapper, and a method left out silently inherits a trait default: maybe_now starts reporting None, or supports_hooks false, changing behaviour rather than failing to compile.

Implement this instead and the delegation is generated:

struct FlushOp<'a>(&'a mut es_entity::DbOp<'static>);

impl<'a> WrapsOperation for FlushOp<'a> {
    type Inner = es_entity::DbOp<'static>;
    fn op(&self) -> &Self::Inner { self.0 }
    fn op_mut(&mut self) -> &mut Self::Inner { self.0 }
}

That is the entire implementation. FlushOp now has time, the clock, the executor, commit hooks, supports_hooks, and — through savepoint_parts — the full SavepointOperation pair including nesting, all with the inner operation’s real capabilities rather than a default. Methods added to AtomicOperation in future cost implementors nothing.

§All or nothing

This is a blanket impl AtomicOperation for W, so a type cannot implement WrapsOperation and override a single method — that would need its own impl AtomicOperation, which would conflict. Wrappers that genuinely differ from the operation they hold write the impl by hand instead; OpWithTime and DbOpWithTime do exactly that, since both override maybe_now to report their cached time.

Use WrapsOperation for pure delegation; hand-write the impl when the wrapper changes behaviour.

Required Associated Types§

Source

type Inner: AtomicOperation + ?Sized

The operation being wrapped.

Required Methods§

Source

fn op(&self) -> &Self::Inner

Shared access, for the &self methods.

Source

fn op_mut(&mut self) -> &mut Self::Inner

Exclusive access, for the &mut self methods.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§