Skip to main content

FnConsumerOnceOps

Trait FnConsumerOnceOps 

Source
pub trait FnConsumerOnceOps<T>: FnOnce(&T) + Sized {
    // Provided method
    fn and_then<C>(self, next: C) -> BoxConsumerOnce<T>
       where Self: 'static,
             C: ConsumerOnce<T> + 'static,
             T: 'static { ... }
}
Expand description

Extension trait providing one-time consumer composition methods for closures

Provides and_then and other composition methods for all closures implementing FnOnce(&T), allowing closures to chain methods directly without explicit wrapper types.

§Features

  • Natural Syntax: Chain operations directly on closures
  • Returns BoxConsumerOnce: Composed results can continue chaining
  • Zero Cost: No overhead when composing closures
  • Automatic Implementation: All FnOnce(&T) closures automatically get these methods

§Examples

use qubit_function::{ConsumerOnce, FnConsumerOnceOps};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let chained = (move |x: &i32| {
    l1.lock().unwrap().push(*x * 2);
}).and_then(move |x: &i32| {
    l2.lock().unwrap().push(*x + 10);
});
chained.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![10, 15]);

§Author

Haixing Hu

Provided Methods§

Source

fn and_then<C>(self, next: C) -> BoxConsumerOnce<T>
where Self: 'static, C: ConsumerOnce<T> + 'static, T: 'static,

Sequentially chain another one-time consumer

Returns a new consumer that executes the current operation first, then the next operation. Consumes the current closure and returns BoxConsumerOnce<T>.

§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. Since BoxConsumerOnce cannot be cloned, the parameter will be consumed. Can be:
    • A closure: |x: &T|
    • A BoxConsumerOnce<T>
    • Any type implementing ConsumerOnce<T>
§Returns

Returns a combined BoxConsumerOnce<T>

§Examples
use qubit_function::{ConsumerOnce, FnConsumerOnceOps};
use std::sync::{Arc, Mutex};

let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let chained = (move |x: &i32| {
    l1.lock().unwrap().push(*x * 2);
}).and_then(move |x: &i32| {
    l2.lock().unwrap().push(*x + 10);
}).and_then(|x: &i32| println!("Result: {}", x));

chained.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![10, 15]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

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

Implement FnConsumerOnceOps for all closure types