This crate allows you to create types which represent closures in const contexts.
To do this simply create an instance of one of the provided closure helpers: Const{Fn, FnMut, FnOnce}Closure
with their associated new
function.
A closure helper instance gets the data to capture (owned for FnOnce
, &mut
for FnMut
and &
for Fn
)
and the closure function to execute.
The closure function must be a const fn
that gets the captured state (owned for FnOnce
, &mut
for FnMut
and &
for Fn
)
and a tuple representing the arguments of the closure.
A closure helper instance returns the return value of the closure function.
```rust
use const_closure::ConstFnMutClosure; const fn imp(state: &mut i32, (arg,): (i32,)) -> i32 { *state += arg; *state } let mut i = 5; let mut cl = ConstFnMutClosure::new(&mut i, imp);
assert!(7 == cl(2)); assert!(8 == cl(1)); ```
Note: The const_closure
macro has been removed in favour of the new generic based approach.
raldone01 and onestacked are the primary authors and maintainers of this library.
This project is released under either:
at your choosing.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.