pub struct BoxConsumerOnce<T> { /* private fields */ }Expand description
BoxConsumerOnce struct
One-time consumer implementation based on Box<dyn FnOnce(&T)> for single ownership scenarios.
This is the simplest consumer type for truly one-time use.
§Features
- Single Ownership: Not cloneable, transfers ownership on use
- Zero Overhead: No reference counting or lock overhead
- One-time Use: Consumes self on first call
- Builder Pattern: Method chaining naturally consumes
self
§Use Cases
Choose BoxConsumerOnce when:
- Consumer is truly used only once
- Building pipelines where ownership flows naturally
- Consumer captures values that should be consumed
- Performance critical and cannot accept shared overhead
§Performance
BoxConsumerOnce has the best performance:
- No reference counting overhead
- No lock acquisition or runtime borrow checking
- Direct function call through vtable
- Minimal memory footprint (single pointer)
§Examples
use prism3_function::{ConsumerOnce, BoxConsumerOnce};
let consumer = BoxConsumerOnce::new(|x: &i32| {
println!("Value: {}", x);
});
consumer.accept(&5);§Author
Haixing Hu
Implementations§
Source§impl<T> BoxConsumerOnce<T>where
T: 'static,
impl<T> BoxConsumerOnce<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) -> BoxConditionalConsumerOnce<T>where
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalConsumerOnce<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) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
T: 'static,
C: ConsumerOnce<T> + 'static,
pub fn and_then<C>(self, after: C) -> BoxConsumerOnce<T>where
Self: Sized + 'static,
T: 'static,
C: ConsumerOnce<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