pub struct RcStatefulBiConsumer<T, U> { /* private fields */ }Expand description
RcStatefulBiConsumer struct
A bi-consumer implementation based on Rc<RefCell<dyn FnMut(&T, &U)>>
for single-threaded shared ownership scenarios. This consumer provides
the benefits of shared ownership without the overhead of thread
safety.
§Features
- Shared Ownership: Cloneable via
Rc, multiple owners allowed - Single-Threaded: Not thread-safe, cannot send across threads
- Interior Mutability: Uses
RefCellfor runtime borrow checking - No Lock Overhead: More efficient than
ArcStatefulBiConsumerfor single-threaded use - Non-Consuming API:
and_thenborrows&self, original remains usable
§Use Cases
Choose RcStatefulBiConsumer when:
- Need to share bi-consumer within a single thread
- Thread safety is not needed
- Performance matters (avoiding lock overhead)
- Single-threaded UI framework event handling
- Building complex single-threaded state machines
§Performance Considerations
RcStatefulBiConsumer performs better than ArcStatefulBiConsumer in single-threaded
scenarios:
- Non-Atomic Counting: clone/drop cheaper than
Arc - No Lock Overhead:
RefCelluses runtime checking, no locks - Better Cache Locality: No atomic operations means better CPU cache behavior
But still has slight overhead compared to BoxStatefulBiConsumer:
- Reference Counting: Though non-atomic, still exists
- Runtime Borrow Checking:
RefCellchecks at runtime
§Safety
RcStatefulBiConsumer is not thread-safe and does not implement Send or
Sync. Attempting to send it to another thread will result in a
compile error. For thread-safe sharing, use ArcStatefulBiConsumer instead.
§Examples
use prism3_function::{BiConsumer, RcStatefulBiConsumer};
use std::rc::Rc;
use std::cell::RefCell;
let log = Rc::new(RefCell::new(Vec::new()));
let l = log.clone();
let mut consumer = RcStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l.borrow_mut().push(*x + *y);
});
let mut clone = consumer.clone();
consumer.accept(&5, &3);
assert_eq!(*log.borrow(), vec![8]);§Author
Haixing Hu
Implementations§
Source§impl<T, U> RcStatefulBiConsumer<T, U>where
T: 'static,
U: 'static,
impl<T, U> RcStatefulBiConsumer<T, U>where
T: 'static,
U: 'static,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new bi-consumer.
Wraps the provided closure in the appropriate smart pointer type for this bi-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 bi-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 bi-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 bi-consumer.
Creates a bi-consumer that does nothing when called. Useful for default values or placeholder implementations.
§Returns
Returns a new bi-consumer instance that performs no operation.
Sourcepub fn when<P>(&self, predicate: P) -> RcConditionalStatefulBiConsumer<T, U>where
P: BiPredicate<T, U> + 'static,
pub fn when<P>(&self, predicate: P) -> RcConditionalStatefulBiConsumer<T, U>where
P: BiPredicate<T, U> + 'static,
Creates a conditional bi-consumer
Wraps this bi-consumer with a bi-predicate condition, creating a new conditional bi-consumer that will only execute the original bi-consumer when the predicate evaluates to true.
§Parameters
predicate- The condition that must be satisfied for the bi-consumer to execute
§Returns
Returns a conditional bi-consumer that executes this bi-consumer only when the predicate is satisfied
§Examples
let consumer = ArcBiConsumer::new(|x: &i32, y: &i32| println!("{}", x + y));
let conditional = consumer.when(|x, y| *x > 0 && *y > 0);
conditional.accept(&5, &3); // prints: 8
conditional.accept(&-5, &3); // prints nothingSourcepub fn and_then<C>(&self, after: C) -> RcStatefulBiConsumer<T, U>where
T: 'static,
U: 'static,
C: StatefulBiConsumer<T, U> + 'static,
pub fn and_then<C>(&self, after: C) -> RcStatefulBiConsumer<T, U>where
T: 'static,
U: 'static,
C: StatefulBiConsumer<T, U> + 'static,
Chains another bi-consumer in sequence
Combines this bi-consumer with another bi-consumer into a new
bi-consumer that executes both bi-consumers in sequence. The returned
bi-consumer first executes this bi-consumer, then unconditionally
executes the after bi-consumer.
§Parameters
after- The bi-consumer to execute after this one (always executed)
§Returns
Returns a new bi-consumer that executes both bi-consumers in sequence
§Examples
let consumer1 = ArcBiConsumer::new(|x: &i32, y: &i32| print!("first: {}", x + y));
let consumer2 = ArcBiConsumer::new(|x: &i32, y: &i32| println!(" second: {}", x * y));
let chained = consumer1.and_then(consumer2);
chained.accept(&5, &3); // prints: first: 8 second: 15