Skip to main content

sliced

Macro sliced 

Source
macro_rules! sliced {
    ($($tt:tt)*) => { ... };
}
Expand description

Transforms a live collection with a computation relying on a slice of another live collection. This is useful for reading a snapshot of an asynchronously updated collection while processing another collection, such as joining a stream with the latest values from a singleton.

§Syntax

The sliced! macro takes in a closure-like syntax specifying the live collections to be sliced and the body of the transformation. Each use statement indicates a live collection to be sliced, along with a non-determinism explanation. The style specifies how the live collection is sliced: use::batch for stream-like collections (such as Stream), use::snapshot for singleton-like collections (such as Singleton), use::atomic for atomically-processed collections, and use::clone (which takes no non-determinism explanation) for reading a bounded singleton-like collection (see Bounded Values). All use statements must appear before the body.

ⓘ
let stream = sliced! {
    let name1 = use::batch(stream1, nondet!(/** explanation */));
    let name2 = use::snapshot(singleton2, nondet!(/** explanation */));
    let name3 = use::atomic(collection3, nondet!(/** explanation */));

    // arbitrary statements can follow
    let intermediate = name1.map(...);
    intermediate.cross_singleton(name2)
};

The style-less form use(collection, nondet!(...)), which picks between batching and snapshotting automatically based on the collection type, is deprecated; use the explicit use::batch or use::snapshot styles instead.

§Bounded Values

A top-level bounded Singleton or Optional (for example, a configuration value fixed when the flow is built) can be read in every slice with use::clone(collection), which takes no non-determinism explanation since the collection has a single immutable value. The binding is a bounded Singleton (or Optional) on the slice’s tick, which can be used like any snapshot (e.g. with cross_singleton or by_ref()). The value is cloned into every tick, so it must implement Clone; avoiding that clone requires snapshots to return references (hydro#2415), and a use::reference style is planned for that. Unbounded collections are rejected at compile time; use use::snapshot for those. See clone for details.

ⓘ
let stream = sliced! {
    let batch = use::batch(stream1, nondet!(/** explanation */));
    let config = use::clone(bounded_config);

    let config = config.by_ref();
    batch.map(q!(|x| x * config.scale))
};

§Stateful Computations

The sliced! macro also supports stateful computations across iterations using let mut bindings with use::state or use::state_null. These create cycles that persist values between iterations.

  • use::state(|l| initial): Creates a cycle with an initial value. The closure receives the slice location and returns the initial state for the first iteration.
  • use::state_null::<Type>(): Creates a cycle that starts as null/empty on the first iteration.

The mutable binding can be reassigned in the body, and the final value will be passed to the next iteration.

ⓘ
let counter_stream = sliced! {
    let batch = use::batch(input_stream, nondet!(/** explanation */));
    let mut counter = use::state(|l| l.singleton(q!(0)));

    // Increment counter by the number of items in this batch
    let new_count = counter.clone().zip(batch.count())
        .map(q!(|(old, add)| old + add));
    counter = new_count.clone();
    new_count.into_stream()
};