Skip to main content

Module store

Module store 

Source
Expand description

The event store port, in two flavours.

§Why there are two traits

async fn in a trait produces an anonymous future whose Send-ness is fixed by the trait definition. Two of happenstance’s targets disagree about that:

  • native / tokio needs Send futures, or tokio::spawn rejects them;
  • wasm32 / Cloudflare Workers is single-threaded and its futures are !Send, so a Send bound cannot be satisfied there at all.

Rather than duplicate the trait, EventStore is written without any Send requirement and trait_variant derives SendEventStore from it. The two are related in exactly one direction, which is the sound one:

    impl SendEventStore  ==>  EventStore comes free
    impl EventStore      =/=> SendEventStore  (cannot conjure Send)

§What that means in practice

  • Writing an adapter? Implement SendEventStore if your store can be shared across threads — which is every native adapter. Implement EventStore only when you genuinely cannot, as on wasm.
  • Writing code that takes a store? Bound on EventStore. It is the weaker requirement, so it accepts both kinds. Reach for SendEventStore only where you actually need to cross a thread boundary.

§Import one flavour, not both

Because a SendEventStore is also an EventStore, bringing both names into scope makes method-call syntax ambiguous:

error[E0034]: multiple applicable items in scope
 --> src/lib.rs:6:19
  |
6 |     let _ = store.read(&query, options);
  |                   ^^^^ multiple `read` found
  |
  = note: candidate #1 is defined in an impl of the trait `SendEventStore` for the type `MemoryEventStore`
  = note: candidate #2 is defined in an impl of the trait `EventStore` for the type `TraitVariantBlanketType`
help: disambiguate the method for candidate #2
  |
6 -     let _ = store.read(&query, options);
6 +     let _ = EventStore::read(&store, &query, options);
  |

In words, because a caret does not survive a search hit or a screen reader: store.read(…) is ambiguous because both EventStore and SendEventStore are in scope and each of them supplies a read.

Import only the one you are binding on — EventStore in almost every case. If you genuinely need both in one module, disambiguate with fully-qualified syntax: SendEventStore::read(&store, &query, options).

The block above is a text fence and nothing compiles it. The error code is asserted by the compiled rust,compile_fail,E0034 examples in standards/rust/20-two-flavour-ports.md and standards/rust/00-prime-directives.md; the notes’ wording and TraitVariantBlanketType are rustc 1.97.1’s, asserted by nothing.

For why there are two flavours at all, and what an adapter looks like once you accept them, the adapter reading order at docs/adapter-reading-order.md sequences what is already written. Named, not linked: it is a page, not an item.

§Naming

The names carry the distinction deliberately. trait_variant’s own convention would call the !Send flavour LocalEventStore, but “local” already means something else here — a local-first application’s on-device store — and conflating the two would be a permanent source of confusion.

Traits§

EventStore
A DCB-compliant event store.
SendEventStore
A DCB-compliant event store.

Functions§

collect
Drains a read stream into a Vec, stopping at the first error.
read_decision_model
Reads query and reports both the matching events and the position to use in a follow-up AppendCondition.