Skip to content

Helpers for creating const callable closures.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

ink-feather-org/const-closure-rs

Repository files navigation

const-closure

docs.rs crates.io rustc

THIS CRATE IS BROKEN - Archive Note

The last tested working rust version is 1.71.0-nightly (nightly-2023-04-18). Most const traits have been removed from the standard library. Once they are back this crate might make a return if it is still needed.

Description

This crate allows you to create types which represent closures in const contexts.

To do this simply create an instance of the provided closure helper: ConstClosure with the associated new function.

A closure helper instance gets the data to capture as a tuple of same ownership (Owned, Mut or ref) and the implementation function to execute.

The implementation function must be a const fn that gets the captured state (a tuple of: owned values 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 on call.

Example

#![feature(const_trait_impl)]
#![feature(const_mut_refs)] // Only required for the FnMut example

use const_closure::ConstClosure;

// FnOnce:
const _: () = {
  const fn imp((state,): (i32,), (arg,): (i32,)) -> i32 {
    state + arg
  }
  let cl = ConstClosure::new((5,), imp);
  assert!(7 == cl(2));
};

// FnMut:
const _: () = {
  const fn imp((state,): (&mut i32,), (arg,): (i32,)) -> i32 {
    *state += arg;
    *state
  }
  let mut i = 5;
  let mut cl = ConstClosure::new((&mut i,), imp);

  assert!(7 == cl(2));
  assert!(8 == cl(1));
};

// Fn:
const _: () = {
  const fn imp((state,): (&i32,), (arg,): (i32,)) -> i32 {
    *state + arg
  }
  let i = 5;
  let mut cl = ConstClosure::new((&i,), imp);
  assert!(7 == cl(2));
  assert!(8 == cl(3));
  assert!(6 == cl(1));
};

Note: The const_closure macro has been removed in favour of the new generic based approach.

Authors

raldone01 and onestacked are the primary authors and maintainers of this library.

License

This project is released under either:

at your choosing.

Contribution

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.

About

Helpers for creating const callable closures.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages