Skip to main content

BoxConsumerOnce

Struct BoxConsumerOnce 

Source
pub struct BoxConsumerOnce<T> { /* private fields */ }
Expand description

BoxConsumerOnce struct

One-time consumer implementation based on Box<dyn FnOnce(&T)> for single ownership scenarios. This is the simplest consumer type for truly one-time use.

§Features

  • Single Ownership: Not cloneable, transfers ownership on use
  • Zero Overhead: No reference counting or lock overhead
  • One-time Use: Consumes self on first call
  • Builder Pattern: Method chaining naturally consumes self

§Use Cases

Choose BoxConsumerOnce when:

  • Consumer is truly used only once
  • Building pipelines where ownership flows naturally
  • Consumer captures values that should be consumed
  • Performance critical and cannot accept shared overhead

§Performance

BoxConsumerOnce has the best performance:

  • No reference counting overhead
  • No lock acquisition or runtime borrow checking
  • Direct function call through vtable
  • Minimal memory footprint (single pointer)

§Examples

use qubit_function::{ConsumerOnce, BoxConsumerOnce};

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

§Author

Haixing Hu

Implementations§

Source§

impl<T> BoxConsumerOnce<T>

Source

pub fn new<F>(f: F) -> Self
where F: FnOnce(&T) + 'static,

Creates a new consumer.

Wraps the provided closure in the appropriate smart pointer type for this consumer implementation.

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

pub fn new_with_name<F>(name: &str, f: F) -> Self
where F: FnOnce(&T) + 'static,

Creates a new named consumer.

Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.

Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where F: FnOnce(&T) + 'static,

Creates a new named consumer with an optional name.

Wraps the provided closure and assigns it an optional name.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this consumer.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the name of this consumer.

§Parameters
  • name - The name to set for this consumer
Source

pub fn clear_name(&mut self)

Clears the name of this consumer.

Source

pub fn noop() -> Self

Creates a no-operation consumer.

Creates a consumer that does nothing when called. Useful for default values or placeholder implementations.

§Returns

Returns a new consumer instance that performs no operation.

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

pub fn when<P>(self, predicate: P) -> BoxConditionalConsumerOnce<T>
where T: 'static, P: Predicate<T> + 'static,

Creates a conditional consumer that executes based on predicate result.

§Parameters
  • predicate - The predicate to determine whether to execute the consumption operation
§Returns

Returns a conditional consumer that only executes when the predicate returns true.

§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use qubit_function::consumers::*;

let counter = Arc::new(AtomicI32::new(0));
let consumer = BoxConsumer::new({
    let counter = Arc::clone(&counter);
    move |value: &i32| {
        counter.fetch_add(*value, Ordering::SeqCst);
    }
});

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

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

Chains execution with another consumer, executing the current consumer first, then the subsequent consumer.

§Parameters
  • after - The subsequent consumer to execute after the current consumer completes
§Returns

Returns a new consumer that executes the current consumer and the subsequent consumer in sequence.

§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use qubit_function::consumers::*;

let counter1 = Arc::new(AtomicI32::new(0));
let counter2 = Arc::new(AtomicI32::new(0));

let consumer1 = BoxConsumer::new({
    let counter = Arc::clone(&counter1);
    move |value: &i32| {
        counter.fetch_add(*value, Ordering::SeqCst);
    }
});

let consumer2 = BoxConsumer::new({
    let counter = Arc::clone(&counter2);
    move |value: &i32| {
        counter.fetch_add(*value * 2, Ordering::SeqCst);
    }
});

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

Trait Implementations§

Source§

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

Source§

fn accept(self, value: &T)

Execute one-time consumption operation Read more
Source§

fn into_box(self) -> BoxConsumerOnce<T>

Convert to BoxConsumerOnce Read more
Source§

fn into_fn(self) -> impl FnOnce(&T)

Convert to closure Read more
Source§

impl<T> Debug for BoxConsumerOnce<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for BoxConsumerOnce<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BoxConsumerOnce<T>

§

impl<T> !RefUnwindSafe for BoxConsumerOnce<T>

§

impl<T> !Send for BoxConsumerOnce<T>

§

impl<T> !Sync for BoxConsumerOnce<T>

§

impl<T> Unpin for BoxConsumerOnce<T>

§

impl<T> UnsafeUnpin for BoxConsumerOnce<T>

§

impl<T> !UnwindSafe for BoxConsumerOnce<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.