Skip to main content

closure_once

Function closure_once 

Source
pub fn closure_once<T, D>(data: D, f: fn(D) -> T) -> ClosureOnce<T, D>
where D: ThreadAware,
Expand description

Constructs a ClosureOnce.

Create a closure-like object by explicitly providing closed-over value and a function pointer to operate on that value, essentially simulating a parameterless closure that ensures that captured data implements ThreadAware.

Usage:

struct Transferable;
impl ThreadAware for Transferable {
    // ...
}

let closure = closure_once(Transferable, |transferable| {
    // do stuff with transferable
});

closure.call_once();

let closure_with_multiple_captured = closure_once((Transferable, Transferable), |(a, b)| {
    // do stuff with a and b
});

closure_with_multiple_captured.call_once();

This exists because Rust closures don’t give us control over the types of captured values.