Skip to main content

ConsumerOnce

Trait ConsumerOnce 

Source
pub trait ConsumerOnce<T> {
    // Required method
    fn accept(self, value: &T);

    // Provided methods
    fn into_box(self) -> BoxConsumerOnce<T>
       where Self: Sized + 'static,
             T: 'static { ... }
    fn into_fn(self) -> impl FnOnce(&T)
       where Self: Sized + 'static { ... }
    fn to_box(&self) -> BoxConsumerOnce<T>
       where Self: Sized + Clone + 'static,
             T: 'static { ... }
    fn to_fn(&self) -> impl FnOnce(&T)
       where Self: Sized + Clone + 'static { ... }
}
Expand description

ConsumerOnce trait - Unified one-time consumer interface

It is similar to the FnOnce(&T) trait in the standard library.

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 qubit_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

Haixing Hu

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 qubit_function::{ConsumerOnce, BoxConsumerOnce};

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

Provided Methods§

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.

§Default Implementation

The default implementation wraps self in a BoxConsumerOnce by calling accept on the consumer. Types can override this method to provide more efficient conversions.

§Returns

Returns the wrapped BoxConsumerOnce<T>

§Examples
use qubit_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,

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.

§Default Implementation

The default implementation creates a closure that captures self and calls its accept method. Types can override this method to provide more efficient conversions.

§Returns

Returns a closure implementing FnOnce(&T)

§Examples
use qubit_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 * 2);
};
let func = closure.into_fn();
func(&5);
assert_eq!(*log.lock().unwrap(), vec![10]);
Source

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

Convert to BoxConsumerOnce without consuming self

⚠️ Requires Clone: This method requires Self to implement Clone. Clones the current consumer and wraps it in a BoxConsumerOnce.

§Default Implementation

The default implementation clones self and then calls into_box() on the clone. Types can override this method to provide more efficient conversions.

§Returns

Returns the wrapped BoxConsumerOnce<T>

§Examples
use qubit_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.to_box();
box_consumer.accept(&5);
assert_eq!(*log.lock().unwrap(), vec![5]);
Source

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

Convert to closure without consuming self

⚠️ Requires Clone: This method requires Self to implement Clone. Clones the current consumer and then converts the clone to a closure.

§Default Implementation

The default implementation clones self and then calls into_fn() on the clone. Types can override this method to provide more efficient conversions.

§Returns

Returns a closure implementing FnOnce(&T)

§Examples
use qubit_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 * 2);
};
let func = closure.to_fn();
func(&5);
assert_eq!(*log.lock().unwrap(), vec![10]);

Implementors§