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().expect("mutex should not be poisoned").push(*x * 2);
}).and_then(move |x: &i32| {
    l2.lock().expect("mutex should not be poisoned").push(*x + 10);
});
chained.accept(&5);
assert_eq!(*log.lock().expect("mutex should not be poisoned"), vec![10, 15]);

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().expect("mutex should not be poisoned").push(*x * 2);
}).and_then(move |x: &i32| {
    l2.lock().expect("mutex should not be poisoned").push(*x + 10);
}).and_then(|x: &i32| println!("Result: {}", x));

chained.accept(&5);
assert_eq!(*log.lock().expect("mutex should not be poisoned"), vec![10, 15]);
Examples found in repository?
examples/consumers/consumer_once_demo.rs (lines 130-133)
24fn main() {
25    println!("=== ConsumerOnce Demo ===\n");
26
27    // 1. BoxConsumerOnce - Single ownership, one-time use
28    println!("1. BoxConsumerOnce - Single ownership");
29    {
30        let log = Arc::new(Mutex::new(Vec::new()));
31        let l = log.clone();
32        let consumer = BoxConsumerOnce::new(move |x: &i32| {
33            l.lock().expect("mutex should not be poisoned").push(*x);
34            println!("  BoxConsumerOnce consumed: {}", x);
35        });
36        consumer.accept(&42);
37        println!("  Log: {:?}\n", *log.lock().expect("mutex should not be poisoned"));
38    }
39
40    // 2. BoxConsumerOnce - Method chaining
41    println!("2. BoxConsumerOnce - Method chaining");
42    {
43        let log = Arc::new(Mutex::new(Vec::new()));
44        let l1 = log.clone();
45        let l2 = log.clone();
46        let l3 = log.clone();
47        let chained = BoxConsumerOnce::new(move |x: &i32| {
48            l1.lock().expect("mutex should not be poisoned").push(*x * 2);
49            println!("  Step 1: {} * 2 = {}", x, x * 2);
50        })
51        .and_then(move |x: &i32| {
52            l2.lock().expect("mutex should not be poisoned").push(*x + 10);
53            println!("  Step 2: {} + 10 = {}", x, x + 10);
54        })
55        .and_then(move |x: &i32| {
56            l3.lock().expect("mutex should not be poisoned").push(*x - 1);
57            println!("  Step 3: {} - 1 = {}", x, x - 1);
58        });
59        chained.accept(&5);
60        println!("  Log: {:?}\n", *log.lock().expect("mutex should not be poisoned"));
61    }
62
63    // 3. BoxConsumerOnce - Factory methods
64    println!("3. BoxConsumerOnce - Factory methods");
65    {
66        // No-op consumer
67        let noop = BoxConsumerOnce::<i32>::noop();
68        noop.accept(&42);
69        println!("  No-op consumer executed (no output)");
70
71        // Print consumer
72        print!("  Print consumer: ");
73        let print = BoxConsumerOnce::new(|x: &i32| println!("{}", x));
74        print.accept(&42);
75
76        // Print with prefix
77        print!("  Print with prefix: ");
78        let print_with = BoxConsumerOnce::new(|x: &i32| println!("Value: {}", x));
79        print_with.accept(&42);
80
81        // Conditional consumer
82        let log = Arc::new(Mutex::new(Vec::new()));
83        let l = log.clone();
84        let conditional = BoxConsumerOnce::new(move |x: &i32| {
85            l.lock().expect("mutex should not be poisoned").push(*x * 2);
86        })
87        .when(|x: &i32| *x > 0);
88        conditional.accept(&5);
89        println!(
90            "  Conditional (positive): {:?}",
91            *log.lock().expect("mutex should not be poisoned")
92        );
93
94        let log = Arc::new(Mutex::new(Vec::new()));
95        let l = log.clone();
96        let conditional = BoxConsumerOnce::new(move |x: &i32| {
97            l.lock().expect("mutex should not be poisoned").push(*x * 2);
98        })
99        .when(|x: &i32| *x > 0);
100        conditional.accept(&-5);
101        println!(
102            "  Conditional (negative): {:?}\n",
103            *log.lock().expect("mutex should not be poisoned")
104        );
105    }
106
107    // 4. Closure usage
108    println!("4. Closure usage");
109    {
110        let log = Arc::new(Mutex::new(Vec::new()));
111        let l = log.clone();
112        let closure = move |x: &i32| {
113            l.lock().expect("mutex should not be poisoned").push(*x * 2);
114            println!("  Closure consumed: {}", x);
115        };
116        closure.accept(&42);
117        println!("  Log: {:?}\n", *log.lock().expect("mutex should not be poisoned"));
118    }
119
120    // 5. Closure chaining
121    println!("5. Closure chaining");
122    {
123        let log = Arc::new(Mutex::new(Vec::new()));
124        let l1 = log.clone();
125        let l2 = log.clone();
126        let chained = (move |x: &i32| {
127            l1.lock().expect("mutex should not be poisoned").push(*x * 2);
128            println!("  Closure 1: {} * 2 = {}", x, x * 2);
129        })
130        .and_then(move |x: &i32| {
131            l2.lock().expect("mutex should not be poisoned").push(*x + 10);
132            println!("  Closure 2: {} + 10 = {}", x, x + 10);
133        });
134        chained.accept(&5);
135        println!("  Log: {:?}\n", *log.lock().expect("mutex should not be poisoned"));
136    }
137
138    // 6. Type conversions
139    println!("6. Type conversions");
140    {
141        let log = Arc::new(Mutex::new(Vec::new()));
142
143        // Closure to BoxConsumerOnce
144        let l = log.clone();
145        let closure = move |x: &i32| {
146            l.lock().expect("mutex should not be poisoned").push(*x);
147        };
148        let box_consumer = closure.into_box();
149        box_consumer.accept(&1);
150        println!(
151            "  BoxConsumerOnce: {:?}",
152            *log.lock().expect("mutex should not be poisoned")
153        );
154    }
155
156    // 7. Using with iterators (BoxConsumerOnce)
157    println!("7. Using with iterators");
158    {
159        let log = Arc::new(Mutex::new(Vec::new()));
160        let l = log.clone();
161        let consumer = BoxConsumerOnce::new(move |x: &i32| {
162            l.lock().expect("mutex should not be poisoned").push(*x * 2);
163        });
164        // Note: This will panic because BoxConsumerOnce can only be called once
165        // vec![1, 2, 3, 4, 5].iter().for_each(consumer.into_fn());
166        consumer.accept(&1);
167        println!(
168            "  BoxConsumerOnce with single value: {:?}\n",
169            *log.lock().expect("mutex should not be poisoned")
170        );
171    }
172
173    println!("=== Demo Complete ===");
174}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

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

Implement FnConsumerOnceOps for all closure types