pub struct ArcConditionalConsumer<T> { /* private fields */ }Expand description
ArcConditionalConsumer struct
A thread-safe conditional consumer that only executes when a predicate is
satisfied. Uses ArcConsumer and ArcPredicate for shared ownership across
threads.
This type is typically created by calling ArcConsumer::when() and is
designed to work with the or_else() method to create if-then-else logic.
§Features
- Shared Ownership: Cloneable via
Arc, multiple owners allowed - Thread-Safe: Implements
Send + Sync, safe for concurrent use - Conditional Execution: Only consumes when predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic
§Examples
use prism3_function::{Consumer, ArcConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let conditional = ArcConsumer::new(move |x: &i32| {
l.lock().unwrap().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.lock().unwrap(), vec![5]);§Author
Hu Haixing
Implementations§
Source§impl<T> ArcConditionalConsumer<T>where
T: Send + 'static,
impl<T> ArcConditionalConsumer<T>where
T: Send + 'static,
Sourcepub fn or_else<C>(&self, else_consumer: C) -> ArcConsumer<T>
pub fn or_else<C>(&self, else_consumer: C) -> ArcConsumer<T>
Adds an else branch (thread-safe 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|(must beSend) ArcConsumer<T>,BoxConsumer<T>- Any type implementing
Consumer<T> + Send
- Closure:
§Returns
Returns the composed ArcConsumer<T>
§Examples
§Using a closure (recommended)
use prism3_function::{Consumer, ArcConsumer};
use std::sync::{Arc, Mutex};
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let mut consumer = ArcConsumer::new(move |x: &i32| {
l1.lock().unwrap().push(*x);
})
.when(|x: &i32| *x > 0)
.or_else(move |x: &i32| {
l2.lock().unwrap().push(-*x);
});
consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);
consumer.accept(&-5);
assert_eq!(*log.lock().unwrap(), vec![5, 5]);Trait Implementations§
Source§impl<T> Clone for ArcConditionalConsumer<T>
impl<T> Clone for ArcConditionalConsumer<T>
Source§impl<T> Consumer<T> for ArcConditionalConsumer<T>where
T: Send + 'static,
impl<T> Consumer<T> for ArcConditionalConsumer<T>where
T: Send + 'static,
Source§fn into_box(self) -> BoxConsumer<T>where
T: 'static,
fn into_box(self) -> BoxConsumer<T>where
T: 'static,
Convert to BoxConsumer Read more
Source§fn into_rc(self) -> RcConsumer<T>where
T: 'static,
fn into_rc(self) -> RcConsumer<T>where
T: 'static,
Convert to RcConsumer 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 ArcConditionalConsumer<T>
impl<T> !RefUnwindSafe for ArcConditionalConsumer<T>
impl<T> Send for ArcConditionalConsumer<T>
impl<T> Sync for ArcConditionalConsumer<T>
impl<T> Unpin for ArcConditionalConsumer<T>
impl<T> !UnwindSafe for ArcConditionalConsumer<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