pub struct BoxStatefulConsumer<T> { /* private fields */ }Expand description
BoxStatefulConsumer struct
Consumer implementation based on Box<dyn FnMut(&T)> for single ownership
scenarios. When sharing is not needed, this is the simplest and most
efficient consumer type.
§Features
- Single Ownership: Not cloneable, transfers ownership when used
- Zero Overhead: No reference counting or lock overhead
- Mutable State: Can modify captured environment through
FnMut - Builder Pattern: Method chaining naturally consumes
self
§Use Cases
Choose BoxStatefulConsumer when:
- Consumer is used only once or in a linear flow
- Building pipelines where ownership flows naturally
- No need to share consumers across contexts
- Performance critical and cannot accept sharing overhead
§Performance
BoxStatefulConsumer has the best performance among the three consumer types:
- No reference counting overhead
- No lock acquisition or runtime borrowing checks
- Direct function calls through vtable
- Minimal memory footprint (single pointer)
§Examples
use prism3_function::{Consumer, BoxStatefulConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let mut consumer = BoxStatefulConsumer::new(move |x: &i32| {
l.lock().unwrap().push(*x);
});
consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);§Author
Haixing Hu
Implementations§
Source§impl<T> BoxStatefulConsumer<T>where
T: 'static,
impl<T> BoxStatefulConsumer<T>where
T: 'static,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new consumer.
Wraps the provided closure in the appropriate smart pointer type for this consumer implementation.
Sourcepub fn new_with_name<F>(name: &str, f: F) -> Self
pub fn new_with_name<F>(name: &str, f: F) -> Self
Creates a new named consumer.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
Creates a new named consumer with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn noop() -> Self
pub fn noop() -> Self
Creates a no-operation consumer.
Creates a consumer that does nothing when called. Useful for default values or placeholder implementations.
§Returns
Returns a new consumer instance that performs no operation.
Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalStatefulConsumer<T>where
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalStatefulConsumer<T>where
P: Predicate<T> + 'static,
Creates a conditional consumer that executes based on predicate result.
§Parameters
predicate- The predicate to determine whether to execute the consumption operation
§Returns
Returns a conditional consumer that only executes when the
predicate returns true.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use prism3_rust_function::consumers::*;
let counter = Arc::new(AtomicI32::new(0));
let consumer = BoxConsumer::new({
let counter = Arc::clone(&counter);
move |value: &i32| {
counter.fetch_add(*value, Ordering::SeqCst);
}
});
let conditional = consumer.when(|value: &i32| *value > 0);
conditional.accept(&1); // counter = 1
conditional.accept(&-1); // not executed, counter remains 1Sourcepub fn and_then<C>(self, after: C) -> BoxStatefulConsumer<T>where
Self: Sized + 'static,
T: 'static,
C: StatefulConsumer<T> + 'static,
pub fn and_then<C>(self, after: C) -> BoxStatefulConsumer<T>where
Self: Sized + 'static,
T: 'static,
C: StatefulConsumer<T> + 'static,
Chains execution with another consumer, executing the current consumer first, then the subsequent consumer.
§Parameters
after- The subsequent consumer to execute after the current consumer completes
§Returns
Returns a new consumer that executes the current consumer and the subsequent consumer in sequence.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use prism3_rust_function::consumers::*;
let counter1 = Arc::new(AtomicI32::new(0));
let counter2 = Arc::new(AtomicI32::new(0));
let consumer1 = BoxConsumer::new({
let counter = Arc::clone(&counter1);
move |value: &i32| {
counter.fetch_add(*value, Ordering::SeqCst);
}
});
let consumer2 = BoxConsumer::new({
let counter = Arc::clone(&counter2);
move |value: &i32| {
counter.fetch_add(*value * 2, Ordering::SeqCst);
}
});
let chained = consumer1.and_then(consumer2);
chained.accept(&5);
// counter1 = 5, counter2 = 10