MovingAverageTransformer

Struct MovingAverageTransformer 

Source
pub struct MovingAverageTransformer { /* private fields */ }
Expand description

A stateful transformer that calculates a moving average over a sliding window.

§Example

use streamweave::transformers::moving_average::MovingAverageTransformer;
use streamweave::transformer::Transformer;
use futures::StreamExt;

let mut transformer = MovingAverageTransformer::new(3); // 3-item window
let input = Box::pin(futures::stream::iter(vec![1.0, 2.0, 3.0, 4.0, 5.0]));
let output = transformer.transform(input);
let results: Vec<f64> = output.collect().await;
// Window: [1] -> avg 1.0
// Window: [1,2] -> avg 1.5
// Window: [1,2,3] -> avg 2.0
// Window: [2,3,4] -> avg 3.0
// Window: [3,4,5] -> avg 4.0
assert_eq!(results, vec![1.0, 1.5, 2.0, 3.0, 4.0]);

Implementations§

Source§

impl MovingAverageTransformer

Source

pub fn new(window_size: usize) -> Self

Creates a new MovingAverageTransformer with the specified window size.

§Arguments
  • window_size - The number of recent items to include in the average.
§Panics

Panics if window_size is 0.

Source

pub fn with_name(self, name: String) -> Self

Sets the name for this transformer.

Source

pub fn with_error_strategy(self, strategy: ErrorStrategy<f64>) -> Self

Sets the error strategy for this transformer.

Source

pub fn window_size(&self) -> usize

Returns the window size.

Trait Implementations§

Source§

impl Clone for MovingAverageTransformer

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MovingAverageTransformer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Input for MovingAverageTransformer

Source§

type Input = f64

The type of items produced by this input stream.
Source§

type InputStream = Pin<Box<dyn Stream<Item = f64> + Send>>

The input stream type that yields items of type Self::Input.
Source§

impl Output for MovingAverageTransformer

Source§

type Output = f64

The type of items produced by this output stream.
Source§

type OutputStream = Pin<Box<dyn Stream<Item = f64> + Send>>

The output stream type that yields items of type Self::Output.
Source§

impl StatefulTransformer for MovingAverageTransformer

Source§

type State = MovingAverageState

The type of state maintained by this transformer.
Source§

type Store = SharedMovingAverageStore

The type of state store used by this transformer.
Source§

fn state_store(&self) -> &Self::Store

Get a reference to the state store.
Source§

fn state_store_mut(&mut self) -> &mut Self::Store

Get a mutable reference to the state store.
Source§

fn state(&self) -> StateResult<Option<Self::State>>

Get the current state value. Read more
Source§

fn set_state(&self, state: Self::State) -> StateResult<()>

Set the state to a new value.
Source§

fn reset_state(&self) -> StateResult<()>

Reset the state to its initial value.
Source§

fn has_state(&self) -> bool

Check if the state has been initialized.
Source§

fn update_state<F>(&self, f: F) -> StateResult<Self::State>
where F: FnOnce(Option<Self::State>) -> Self::State + Send + 'static,

Update the state using a closure. Read more
Source§

fn state_or_initial(&self) -> Result<Self::State, StateError>

Get the current state or the initial state if not set. Read more
Source§

impl Transformer for MovingAverageTransformer

Source§

type InputPorts = (f64,)

The input port tuple type for this transformer. Read more
Source§

type OutputPorts = (f64,)

The output port tuple type for this transformer. Read more
Source§

fn transform(&mut self, input: Self::InputStream) -> Self::OutputStream

Transforms a stream of input items into a stream of output items. Read more
Source§

fn set_config_impl(&mut self, config: TransformerConfig<f64>)

Sets the configuration implementation. Read more
Source§

fn get_config_impl(&self) -> &TransformerConfig<f64>

Returns a reference to the configuration implementation. Read more
Source§

fn get_config_mut_impl(&mut self) -> &mut TransformerConfig<f64>

Returns a mutable reference to the configuration implementation. Read more
Source§

fn handle_error(&self, error: &StreamError<f64>) -> ErrorAction

Handles an error that occurred during stream processing. Read more
Source§

fn create_error_context(&self, item: Option<f64>) -> ErrorContext<f64>

Creates an error context for error reporting. Read more
Source§

fn component_info(&self) -> ComponentInfo

Returns information about the component for error reporting. Read more
Source§

fn with_config(&self, config: TransformerConfig<Self::Input>) -> Self
where Self: Sized + Clone,

Creates a new transformer instance with the given configuration. Read more
Source§

fn set_config(&mut self, config: TransformerConfig<Self::Input>)

Sets the configuration for this transformer. Read more
Source§

fn config(&self) -> &TransformerConfig<Self::Input>

Returns a reference to the transformer’s configuration. Read more
Source§

fn config_mut(&mut self) -> &mut TransformerConfig<Self::Input>

Returns a mutable reference to the transformer’s configuration. Read more
Source§

fn with_name(self, name: String) -> Self
where Self: Sized,

Sets the name for this transformer. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TransformerPorts for T
where T: Transformer, <T as Input>::Input: Debug + Clone + Send + Sync,

Source§

type DefaultInputPorts = (<T as Input>::Input,)

The default input port tuple type (single port with the transformer’s input type).
Source§

type DefaultOutputPorts = (<T as Output>::Output,)

The default output port tuple type (single port with the transformer’s output type).
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V