Skip to main content

JoinBuilder

Struct JoinBuilder 

Source
pub struct JoinBuilder<O> { /* private fields */ }
Expand description

Configures a multi-input join transform.

Available on every runtime. The fan-in queue (bounded channel between input forwarders and the trigger loop) lives in core, with capacity 64 on std and wasm32 and 16 on embedded no_std.

Obtain via RecordRegistrar::transform_join.

Implementations§

Source§

impl<O> JoinBuilder<O>
where O: Send + Sync + Clone + Debug + 'static,

Source

pub fn input<I>(self, key: impl RecordKey) -> Self
where I: Send + Sync + Clone + Debug + 'static,

Add a typed input to the join.

Source

pub fn on_triggers<F, Fut>(self, handler: F) -> JoinPipeline<O>
where F: FnOnce(JoinEventRx, Producer<O>) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static,

Complete the pipeline by providing an async task that owns the event loop and state.

The closure receives a JoinEventRx to read trigger events and a Producer<O> to emit output values. Both are owned — moved into the async move block — so the closure can freely hold borrows across .await points and maintain any state it needs.

The task runs until all input forwarders close (i.e., all upstream records stop producing).

Illustrative (not compiled: fragment of a transform_join pipeline with user-defined input types):

ⓘ
.on_triggers(|mut rx, producer| async move {
    let mut last_a: Option<f32> = None;
    let mut last_b: Option<f32> = None;
    while let Ok(trigger) = rx.recv().await {
        match trigger.index() {
            0 => last_a = trigger.as_input::<InputA>().copied(),
            1 => last_b = trigger.as_input::<InputB>().copied(),
            _ => {}
        }
        if let (Some(a), Some(b)) = (last_a, last_b) {
            producer.produce(compute(a, b));
        }
    }
})
§Pairing values that belong together

The handler above pairs each new value with the other input’s latest. When both inputs change at about the same time, the first output pairs a new value with an old one. If the inputs carry a field that says which values belong together, emit only when it matches and consume both values:

ⓘ
.on_triggers(|mut rx, producer| async move {
    let mut flow: Option<Flow> = None;
    let mut ret: Option<Return> = None;
    while let Ok(trigger) = rx.recv().await {
        match trigger.index() {
            0 => flow = trigger.as_input::<Flow>().cloned(),
            1 => ret = trigger.as_input::<Return>().cloned(),
            _ => {}
        }
        if let (Some(f), Some(r)) = (&flow, &ret) {
            // Or a tolerance on an application timestamp. `trigger.stamp()`
            // is the write time: "written within", not "measured within".
            if f.sample_id == r.sample_id {
                producer.produce(Difference::of(f, r));
                flow = None;
                ret = None;
            }
        }
    }
})
  • One value is retained per input, so an input running ahead skips cycles: Flow 2, Flow 3, Return 3 emits only cycle 3.
  • Which cycles are skipped depends on the arrival order between inputs.
  • Values produced together by one source are better published as one compound record, which needs no join.

transform_join_pairs_values_by_timestamp in the Tokio adapter’s integration tests runs this handler, matching on timestamps.

Auto Trait Implementations§

§

impl<O> !RefUnwindSafe for JoinBuilder<O>

§

impl<O> !UnwindSafe for JoinBuilder<O>

§

impl<O> Freeze for JoinBuilder<O>
where PhantomData<O>: Freeze,

§

impl<O> Send for JoinBuilder<O>
where PhantomData<O>: Send,

§

impl<O> Sync for JoinBuilder<O>
where PhantomData<O>: Sync,

§

impl<O> Unpin for JoinBuilder<O>
where PhantomData<O>: Unpin,

§

impl<O> UnsafeUnpin for JoinBuilder<O>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.