pub struct RcConditionalConsumer<T> { /* private fields */ }Expand description
RcConditionalConsumer struct
A single-threaded conditional consumer that only executes when a predicate is
satisfied. Uses RcConsumer and RcPredicate for shared ownership within a
single thread.
This type is typically created by calling RcConsumer::when() and is
designed to work with the or_else() method to create if-then-else logic.
§Features
- Shared Ownership: Cloneable via
Rc, multiple owners allowed - Single-Threaded: Not thread-safe, cannot be sent across threads
- Conditional Execution: Only consumes when predicate returns
true - No Lock Overhead: More efficient than
ArcConditionalConsumer
§Examples
use prism3_function::{Consumer, RcConsumer};
use std::rc::Rc;
use std::cell::RefCell;
let log = Rc::new(RefCell::new(Vec::new()));
let l = log.clone();
let conditional = RcConsumer::new(move |x: &i32| {
l.borrow_mut().push(*x);
})
.when(|x: &i32| *x > 0);
let conditional_clone = conditional.clone();
let mut value = 5;
let mut m = conditional;
m.accept(&value);
assert_eq!(*log.borrow(), vec![5]);§Author
Hu Haixing
Implementations§
Source§impl<T> RcConditionalConsumer<T>where
T: 'static,
impl<T> RcConditionalConsumer<T>where
T: 'static,
Sourcepub fn or_else<C>(&self, else_consumer: C) -> RcConsumer<T>where
C: Consumer<T> + 'static,
pub fn or_else<C>(&self, else_consumer: C) -> RcConsumer<T>where
C: Consumer<T> + 'static,
Adds an else branch (single-threaded shared version)
Executes the original consumer when the condition is satisfied, otherwise executes else_consumer.
§Parameters
else_consumer- The consumer for the else branch, can be:- Closure:
|x: &T| RcConsumer<T>,BoxConsumer<T>- Any type implementing
Consumer<T>
- Closure:
§Returns
Returns the composed RcConsumer<T>
§Examples
§Using a closure (recommended)
use prism3_function::{Consumer, RcConsumer};
use std::rc::Rc;
use std::cell::RefCell;
let log = Rc::new(RefCell::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let mut consumer = RcConsumer::new(move |x: &i32| {
l1.borrow_mut().push(*x);
})
.when(|x: &i32| *x > 0)
.or_else(move |x: &i32| {
l2.borrow_mut().push(-*x);
});
consumer.accept(&5);
assert_eq!(*log.borrow(), vec![5]);
consumer.accept(&-5);
assert_eq!(*log.borrow(), vec![5, 5]);Trait Implementations§
Source§impl<T> Clone for RcConditionalConsumer<T>
impl<T> Clone for RcConditionalConsumer<T>
Source§impl<T> Consumer<T> for RcConditionalConsumer<T>where
T: 'static,
impl<T> Consumer<T> for RcConditionalConsumer<T>where
T: 'static,
Source§fn into_box(self) -> BoxConsumer<T>
fn into_box(self) -> BoxConsumer<T>
Convert to BoxConsumer Read more
Source§fn into_rc(self) -> RcConsumer<T>
fn into_rc(self) -> RcConsumer<T>
Convert to RcConsumer Read more
Source§fn into_arc(self) -> ArcConsumer<T>
fn into_arc(self) -> ArcConsumer<T>
Convert to ArcConsumer Read more
Source§fn to_box(&self) -> BoxConsumer<T>
fn to_box(&self) -> BoxConsumer<T>
Convert to BoxConsumer Read more
Source§fn to_rc(&self) -> RcConsumer<T>
fn to_rc(&self) -> RcConsumer<T>
Convert to RcConsumer Read more
Source§fn to_arc(&self) -> ArcConsumer<T>
fn to_arc(&self) -> ArcConsumer<T>
Convert to ArcConsumer Read more
Auto Trait Implementations§
impl<T> Freeze for RcConditionalConsumer<T>
impl<T> !RefUnwindSafe for RcConditionalConsumer<T>
impl<T> !Send for RcConditionalConsumer<T>
impl<T> !Sync for RcConditionalConsumer<T>
impl<T> Unpin for RcConditionalConsumer<T>
impl<T> !UnwindSafe for RcConditionalConsumer<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more