pub struct BoxReadonlyConsumer<T> { /* private fields */ }Expand description
BoxReadonlyConsumer struct
Readonly consumer implementation based on Box<dyn Fn(&T)> for single
ownership scenarios.
§Features
- Single Ownership: Not cloneable, transfers ownership when used
- Zero Overhead: No reference counting or lock overhead
- Completely Immutable: Neither modifies itself nor input
- No Interior Mutability: No need for Mutex or RefCell
§Use Cases
Choose BoxReadonlyConsumer when:
- Readonly consumer is used once or in a linear flow
- No need to share consumer across contexts
- Pure observation operations, such as logging
§Examples
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};
let consumer = BoxReadonlyConsumer::new(|x: &i32| {
println!("Observed value: {}", x);
});
consumer.accept(&5);§Author
Hu Haixing
Implementations§
Source§impl<T> BoxReadonlyConsumer<T>where
T: 'static,
impl<T> BoxReadonlyConsumer<T>where
T: 'static,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Create a new BoxReadonlyConsumer
§Type Parameters
F- Closure type
§Parameters
f- Closure to wrap
§Returns
Returns a new BoxReadonlyConsumer<T> instance
§Examples
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};
let consumer = BoxReadonlyConsumer::new(|x: &i32| {
println!("Value: {}", x);
});
consumer.accept(&5);Sourcepub fn and_then<C>(self, next: C) -> Selfwhere
C: ReadonlyConsumer<T> + 'static,
pub fn and_then<C>(self, next: C) -> Selfwhere
C: ReadonlyConsumer<T> + 'static,
Sequentially chain another readonly consumer
Returns a new consumer that executes the current operation first, then the next operation. Consumes self.
§Type Parameters
C- Type of the next consumer
§Parameters
next- Consumer to execute after the current operation. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original consumer, clone it first (if it implementsClone). Can be:- A closure:
|x: &T| - A
BoxReadonlyConsumer<T> - An
RcReadonlyConsumer<T> - An
ArcReadonlyConsumer<T> - Any type implementing
ReadonlyConsumer<T>
- A closure:
§Returns
Returns a new combined BoxReadonlyConsumer<T>
§Examples
§Direct value passing (ownership transfer)
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer};
let first = BoxReadonlyConsumer::new(|x: &i32| {
println!("First: {}", x);
});
let second = BoxReadonlyConsumer::new(|x: &i32| {
println!("Second: {}", x);
});
// second is moved here
let chained = first.and_then(second);
chained.accept(&5);
// second.accept(&3); // Would not compile - moved§Preserving original with clone
use prism3_function::{ReadonlyConsumer, BoxReadonlyConsumer,
RcReadonlyConsumer};
let first = BoxReadonlyConsumer::new(|x: &i32| {
println!("First: {}", x);
});
let second = RcReadonlyConsumer::new(|x: &i32| {
println!("Second: {}", x);
});
// Clone to preserve original
let chained = first.and_then(second.clone());
chained.accept(&5);
// Original still usable
second.accept(&3);Trait Implementations§
Source§impl<T> Debug for BoxReadonlyConsumer<T>
impl<T> Debug for BoxReadonlyConsumer<T>
Source§impl<T> Display for BoxReadonlyConsumer<T>
impl<T> Display for BoxReadonlyConsumer<T>
Source§impl<T> ReadonlyConsumer<T> for BoxReadonlyConsumer<T>
impl<T> ReadonlyConsumer<T> for BoxReadonlyConsumer<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
Auto Trait Implementations§
impl<T> Freeze for BoxReadonlyConsumer<T>
impl<T> !RefUnwindSafe for BoxReadonlyConsumer<T>
impl<T> !Send for BoxReadonlyConsumer<T>
impl<T> !Sync for BoxReadonlyConsumer<T>
impl<T> Unpin for BoxReadonlyConsumer<T>
impl<T> !UnwindSafe for BoxReadonlyConsumer<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