ConsumerOnce

Trait ConsumerOnce 

Source
pub trait ConsumerOnce<T> {
    // Required methods
    fn accept(self, value: &T);
    fn into_box(self) -> BoxConsumerOnce<T>
       where Self: Sized + 'static,
             T: 'static;
    fn into_fn(self) -> impl FnOnce(&T)
       where Self: Sized + 'static,
             T: 'static;
}
Expand description

ConsumerOnce trait - Unified one-time consumer interface

Defines the core behavior of all one-time consumer types. Similar to consumers implementing FnOnce(&T), executes operations that accept a value reference but return no result (only side effects), consuming itself in the process.

§Automatic Implementation

  • All closures implementing FnOnce(&T)
  • BoxConsumerOnce<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: Can be converted to BoxConsumerOnce
  • Generic Programming: Write functions that work with any one-time consumer type

§Examples

use prism3_function::{ConsumerOnce, BoxConsumerOnce};
use std::sync::{Arc, Mutex};

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

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

§Author

Hu Haixing

Required Methods§

Source

fn accept(self, value: &T)

Execute one-time consumption operation

Executes 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. Consumes self.

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

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

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

Convert to BoxConsumerOnce

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

§Returns

Returns the wrapped BoxConsumerOnce<T>

§Examples
use prism3_function::ConsumerOnce;
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 box_consumer = closure.into_box();
box_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);
Source

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

Convert to closure

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

Converts a one-time consumer to a closure that can be used directly in places where the standard library requires FnOnce.

§Returns

Returns a closure implementing FnOnce(&T)

Implementors§

Source§

impl<T> ConsumerOnce<T> for BoxConditionalConsumerOnce<T>
where T: 'static,

Source§

impl<T> ConsumerOnce<T> for BoxConsumerOnce<T>

Source§

impl<T, F> ConsumerOnce<T> for F
where F: FnOnce(&T),

Implement ConsumerOnce for all FnOnce(&T)