Consumer

Trait Consumer 

Source
pub trait Consumer<T> {
    // Required methods
    fn accept(&mut self, value: &T);
    fn into_box(self) -> BoxConsumer<T>
       where Self: Sized + 'static,
             T: 'static;
    fn into_rc(self) -> RcConsumer<T>
       where Self: Sized + 'static,
             T: 'static;
    fn into_arc(self) -> ArcConsumer<T>
       where Self: Sized + Send + 'static,
             T: Send + 'static;
    fn into_fn(self) -> impl FnMut(&T)
       where Self: Sized + 'static,
             T: 'static;
}
Expand description

Consumer trait - Unified consumer interface

Defines the core behavior of all consumer types. Similar to Java’s Consumer<T> interface, executes operations that accept a value but return no result (side effects only).

Consumer can modify its own state (such as accumulation, counting), but should not modify the consumed value itself.

§Automatic Implementation

  • All closures implementing FnMut(&T)
  • BoxConsumer<T>, ArcConsumer<T>, RcConsumer<T>

§Features

  • Unified Interface: All consumer types share the same accept method signature
  • Automatic Implementation: Closures automatically implement this trait with zero overhead
  • Type Conversion: Easy conversion between different ownership models
  • Generic Programming: Write functions that work with any consumer type

§Examples

use prism3_function::{Consumer, BoxConsumer, ArcConsumer};
use std::sync::{Arc, Mutex};

fn apply_consumer<C: Consumer<i32>>(consumer: &mut C, value: &i32) {
    consumer.accept(value);
}

// Works with any consumer type
let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let mut box_con = BoxConsumer::new(move |x: &i32| {
    l.lock().unwrap().push(*x);
});
apply_consumer(&mut box_con, &5);
assert_eq!(*log.lock().unwrap(), vec![5]);

§Author

Hu Haixing

Required Methods§

Source

fn accept(&mut self, value: &T)

Execute consumption operation

Performs an operation on the given reference. The operation typically reads the input value or produces side effects, but does not modify the input value itself. Can modify the consumer’s own state.

§Parameters
  • value - Reference to the value to be consumed
§Examples
use prism3_function::{Consumer, BoxConsumer};

let mut consumer = BoxConsumer::new(|x: &i32| println!("{}", x));
let value = 5;
consumer.accept(&value);
Source

fn into_box(self) -> BoxConsumer<T>
where Self: Sized + 'static, T: 'static,

Convert to BoxConsumer

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

Converts the current consumer to BoxConsumer<T>.

§Ownership

This method consumes the consumer (takes ownership of self). After calling this method, the original consumer is no longer available.

Tip: For cloneable consumers (ArcConsumer, RcConsumer), if you need to preserve the original object, you can call .clone() first.

§Return Value

Returns the wrapped BoxConsumer<T>

§Examples
use prism3_function::Consumer;
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let closure = move |x: &i32| {
    l.lock().unwrap().push(*x);
};
let mut box_consumer = closure.into_box();
box_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);
Source

fn into_rc(self) -> RcConsumer<T>
where Self: Sized + 'static, T: 'static,

Convert to RcConsumer

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

§Return Value

Returns the wrapped RcConsumer<T>

Source

fn into_arc(self) -> ArcConsumer<T>
where Self: Sized + Send + 'static, T: Send + 'static,

Convert to ArcConsumer

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

§Return Value

Returns the wrapped ArcConsumer<T>

Source

fn into_fn(self) -> impl FnMut(&T)
where Self: Sized + 'static, T: 'static,

Convert to closure

⚠️ Consumes self: The original consumer will be unavailable after calling this method.

Converts the consumer to a closure that can be used directly in standard library functions requiring FnMut.

§Return Value

Returns a closure implementing FnMut(&T)

§Examples
use prism3_function::{Consumer, BoxConsumer};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l = log.clone();
let consumer = BoxConsumer::new(move |x: &i32| {
    l.lock().unwrap().push(*x);
});
let mut func = consumer.into_fn();
func(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);

Implementors§

Source§

impl<T> Consumer<T> for ArcConditionalConsumer<T>
where T: Send + 'static,

Source§

impl<T> Consumer<T> for ArcConsumer<T>

Source§

impl<T> Consumer<T> for BoxConditionalConsumer<T>
where T: 'static,

Source§

impl<T> Consumer<T> for BoxConsumer<T>

Source§

impl<T> Consumer<T> for RcConditionalConsumer<T>
where T: 'static,

Source§

impl<T> Consumer<T> for RcConsumer<T>

Source§

impl<T, F> Consumer<T> for F
where F: FnMut(&T),

Implement Consumer for all FnMut(&T)