Skip to main content

RetractableAccumulator

Trait RetractableAccumulator 

Source
pub trait RetractableAccumulator:
    Default
    + Clone
    + Send {
    type Input;
    type Output;

    // Required methods
    fn add(&mut self, value: Self::Input);
    fn retract(&mut self, value: &Self::Input);
    fn merge(&mut self, other: &Self);
    fn result(&self) -> Self::Output;
    fn is_empty(&self) -> bool;
    fn reset(&mut self);

    // Provided method
    fn supports_efficient_retraction(&self) -> bool { ... }
}
Expand description

Extension trait for accumulators that support retractions.

Retractable accumulators can “un-apply” a value, which is essential for:

  • Late data corrections (emit -old, +new pairs)
  • Cascading materialized views
  • Changelog-based downstream consumers

§Retraction Efficiency

Some aggregators support O(1) retraction (count, sum, avg), while others may require O(n) recomputation (min, max without value tracking). Use supports_efficient_retraction() to check.

Required Associated Types§

Source

type Input

The input type for the aggregation.

Source

type Output

The output type produced by the aggregation.

Required Methods§

Source

fn add(&mut self, value: Self::Input)

Adds a value to the accumulator.

Source

fn retract(&mut self, value: &Self::Input)

Retracts (un-applies) a value from the accumulator.

This is the inverse of add. For example:

  • Count: decrement by 1
  • Sum: subtract the value
  • Avg: update sum and count
Source

fn merge(&mut self, other: &Self)

Merges another accumulator into this one.

Source

fn result(&self) -> Self::Output

Extracts the final result from the accumulator.

Source

fn is_empty(&self) -> bool

Returns true if the accumulator is empty (no values added).

Source

fn reset(&mut self)

Resets the accumulator to its initial state.

Provided Methods§

Source

fn supports_efficient_retraction(&self) -> bool

Returns true if this accumulator can efficiently retract.

Some aggregators (like Min/Max without value tracking) may need to scan all values on retraction if the retracted value was the current min/max.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§