pub struct ArcReadonlyConsumer<T> { /* private fields */ }Expand description
ArcReadonlyConsumer 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 ArcReadonlyConsumer 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, ArcReadonlyConsumer has no Mutex lock overhead,
performing better in high-concurrency scenarios.
§Examples
use prism3_function::{ReadonlyConsumer, ArcReadonlyConsumer};
let consumer = ArcReadonlyConsumer::new(|x: &i32| {
println!("Observed: {}", x);
});
let clone = consumer.clone();
consumer.accept(&5);
clone.accept(&10);§Author
Hu Haixing
Implementations§
Source§impl<T> ArcReadonlyConsumer<T>
impl<T> ArcReadonlyConsumer<T>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Create a new ArcReadonlyConsumer
§Type Parameters
F- Closure type
§Parameters
f- Closure to wrap
§Returns
Returns a new ArcReadonlyConsumer<T> instance
§Examples
use prism3_function::{ReadonlyConsumer, ArcReadonlyConsumer};
let consumer = ArcReadonlyConsumer::new(|x: &i32| {
println!("Value: {}", x);
});
consumer.accept(&5);Sourcepub fn and_then(&self, next: &ArcReadonlyConsumer<T>) -> ArcReadonlyConsumer<T>
pub fn and_then(&self, next: &ArcReadonlyConsumer<T>) -> ArcReadonlyConsumer<T>
Sequentially chain another ArcReadonlyConsumer
Returns a new consumer that executes the current operation first, then the next operation. Borrows &self, does not consume the original consumer.
§Parameters
next- Consumer to execute after the current operation. Note: This parameter is passed by reference, so the original consumer remains usable. Can be:- An
ArcReadonlyConsumer<T>(passed by reference) - Any type implementing
ReadonlyConsumer<T> + Send + Sync
- An
§Returns
Returns a new combined ArcReadonlyConsumer<T>
§Examples
use prism3_function::{ReadonlyConsumer, ArcReadonlyConsumer};
let first = ArcReadonlyConsumer::new(|x: &i32| {
println!("First: {}", x);
});
let second = ArcReadonlyConsumer::new(|x: &i32| {
println!("Second: {}", x);
});
// second is passed by reference, so it remains usable
let chained = first.and_then(&second);
// first and second remain usable after chaining
chained.accept(&5);
first.accept(&3); // Still usable
second.accept(&7); // Still usableTrait Implementations§
Source§impl<T> Clone for ArcReadonlyConsumer<T>
impl<T> Clone for ArcReadonlyConsumer<T>
Source§impl<T> Debug for ArcReadonlyConsumer<T>
impl<T> Debug for ArcReadonlyConsumer<T>
Source§impl<T> Display for ArcReadonlyConsumer<T>
impl<T> Display for ArcReadonlyConsumer<T>
Source§impl<T> ReadonlyConsumer<T> for ArcReadonlyConsumer<T>
impl<T> ReadonlyConsumer<T> for ArcReadonlyConsumer<T>
Source§fn into_box(self) -> BoxReadonlyConsumer<T>where
T: 'static,
fn into_box(self) -> BoxReadonlyConsumer<T>where
T: 'static,
Convert to BoxReadonlyConsumer Read more
Source§fn into_rc(self) -> RcReadonlyConsumer<T>where
T: 'static,
fn into_rc(self) -> RcReadonlyConsumer<T>where
T: 'static,
Convert to RcReadonlyConsumer Read more
Source§fn into_arc(self) -> ArcReadonlyConsumer<T>
fn into_arc(self) -> ArcReadonlyConsumer<T>
Convert to ArcReadonlyConsumer Read more
Source§fn to_box(&self) -> BoxReadonlyConsumer<T>where
T: 'static,
fn to_box(&self) -> BoxReadonlyConsumer<T>where
T: 'static,
Non-consuming conversion to
BoxReadonlyConsumer Read moreSource§fn to_rc(&self) -> RcReadonlyConsumer<T>where
T: 'static,
fn to_rc(&self) -> RcReadonlyConsumer<T>where
T: 'static,
Non-consuming conversion to
RcReadonlyConsumer Read moreSource§fn to_arc(&self) -> ArcReadonlyConsumer<T>
fn to_arc(&self) -> ArcReadonlyConsumer<T>
Non-consuming conversion to
ArcReadonlyConsumer Read moreAuto Trait Implementations§
impl<T> Freeze for ArcReadonlyConsumer<T>
impl<T> !RefUnwindSafe for ArcReadonlyConsumer<T>
impl<T> Send for ArcReadonlyConsumer<T>
impl<T> Sync for ArcReadonlyConsumer<T>
impl<T> Unpin for ArcReadonlyConsumer<T>
impl<T> !UnwindSafe for ArcReadonlyConsumer<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