const-anonymous-functions

Crates.ioconst-anonymous-functions
lib.rsconst-anonymous-functions
version1.1.0
sourcesrc
created_at2023-08-06 10:38:03.293558
updated_at2023-08-07 12:36:33.879014
descriptionSimple macro to create const anonymous functions
homepage
repositoryhttps://github.com/Odilf/const_anonymous_functions
max_upload_size
id937069
size3,440
(Odilf)

documentation

https://docs.rs/const_anonymous_functions

README

const-anonymous-functions

Small crate that provides a macro to create anonymous functions that can be used in const contexts.

It doesn't provide any new functionality, only syntax sugar.

Usage

Add it to your project

cargo add const-anonymous-functions

Example

use const_anonymous_functions::caf;

const RESULT: i32 = caf!(|a: i32, b: i32| -> i32 { a + b })(1, 2);

assert_eq!(RESULT, 1 + 2);

Caveats

There are a couple caveats to using this crate:

  • You must always annotate the type of the function (both arguments and return type)
  • To avoid confusion, you must always use braces in the function body, so something like |a: i32| a + 1 is not allowed (notice how it would be wrong either way since the return type here is (), so it doesn't return anything). |a: i32| -> i32 { a + 1 } is the correct way to write it.

How it works

This macro is actually super basic. It just takes the closure syntax and transforms it into a const function. Then it returns the function.

So caf!(|a: i32, b: i32| -> i32 { a + b }) literally becomes:

{
	const fn __annon_caf__(a: i32, b: i32) -> i32 {
		a + b
	}

	__annon_caf__
}
# ;

That's it.

Commit count: 0

cargo fmt