pub struct ArcConsumer<T> { /* private fields */ }Expand description
ArcConsumer struct
Readonly consumer implementation based on Arc<dyn Fn(&T) + Send + Sync>,
for thread-safe shared ownership scenarios. No Mutex needed because
operations are readonly.
§Features
- Shared Ownership: Cloneable through
Arc, allows multiple owners - Thread Safe: Implements
Send + Sync, can be safely used concurrently - Lock-free: No Mutex protection needed because it’s readonly
- Non-consuming API:
and_thenborrows&self, original object remains usable
§Use Cases
Choose ArcConsumer when:
- Need to share readonly consumer across multiple threads
- Pure observation operations, such as logging, monitoring, notifications
- Need high-concurrency reads with no lock overhead
§Performance Advantages
Compared to ArcConsumer, ArcConsumer has no Mutex lock overhead,
performing better in high-concurrency scenarios.
§Examples
use prism3_function::{Consumer, ArcConsumer};
let consumer = ArcConsumer::new(|x: &i32| {
println!("Observed: {}", x);
});
let clone = consumer.clone();
consumer.accept(&5);
clone.accept(&10);§Author
Haixing Hu
Implementations§
Source§impl<T> ArcConsumer<T>where
T: 'static,
impl<T> ArcConsumer<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) -> ArcConditionalConsumer<T>
pub fn when<P>(&self, predicate: P) -> ArcConditionalConsumer<T>
Creates a conditional consumer
Wraps this consumer with a predicate condition, creating a new conditional consumer that will only execute the original consumer when the predicate evaluates to true.
§Parameters
predicate- The condition that must be satisfied for the consumer to execute
§Returns
Returns a conditional consumer that executes this consumer only when the predicate is satisfied
§Examples
let consumer = ArcConsumer::new(|x: &i32| println!("{}", x));
let conditional = consumer.when(|x| *x > 0);
conditional.accept(&5); // prints: 5
conditional.accept(&-5); // prints nothingSourcepub fn and_then<C>(&self, after: C) -> ArcConsumer<T>
pub fn and_then<C>(&self, after: C) -> ArcConsumer<T>
Chains another consumer in sequence
Combines this consumer with another consumer into a new consumer
that executes both consumers in sequence. The returned consumer
first executes this consumer, then unconditionally executes the
after consumer.
§Parameters
after- The consumer to execute after this one (always executed)
§Returns
Returns a new consumer that executes both consumers in sequence
§Examples
let consumer1 = ArcConsumer::new(|x: &i32| print!("first: {}", x));
let consumer2 = ArcConsumer::new(|x: &i32| println!(" second: {}", x));
let chained = consumer1.and_then(consumer2);
chained.accept(&5); // prints: first: 5 second: 5Trait Implementations§
Source§impl<T> Clone for ArcConsumer<T>
impl<T> Clone for ArcConsumer<T>
Source§impl<T> Consumer<T> for ArcConsumer<T>
impl<T> Consumer<T> for ArcConsumer<T>
Source§fn into_box(self) -> BoxConsumer<T>where
T: 'static,
fn into_box(self) -> BoxConsumer<T>where
T: 'static,
Source§fn into_rc(self) -> RcConsumer<T>where
T: 'static,
fn into_rc(self) -> RcConsumer<T>where
T: 'static,
Source§fn into_arc(self) -> ArcConsumer<T>where
T: 'static,
fn into_arc(self) -> ArcConsumer<T>where
T: 'static,
Source§fn into_once(self) -> BoxConsumerOnce<T>where
T: 'static,
fn into_once(self) -> BoxConsumerOnce<T>where
T: 'static,
Source§fn to_box(&self) -> BoxConsumer<T>where
T: 'static,
fn to_box(&self) -> BoxConsumer<T>where
T: 'static,
BoxConsumer Read moreSource§fn to_rc(&self) -> RcConsumer<T>where
T: 'static,
fn to_rc(&self) -> RcConsumer<T>where
T: 'static,
RcConsumer Read moreSource§fn to_arc(&self) -> ArcConsumer<T>where
T: 'static,
fn to_arc(&self) -> ArcConsumer<T>where
T: 'static,
ArcConsumer Read more