| Crates.io | fn-item |
| lib.rs | fn-item |
| version | 0.1.1 |
| created_at | 2025-03-19 15:29:25.40227+00 |
| updated_at | 2025-03-19 15:43:49.277664+00 |
| description | Helpers for working with closures that don’t capture any variables. |
| homepage | |
| repository | https://github.com/SabrinaJewson/fn-item.rs |
| max_upload_size | |
| id | 1598144 |
| size | 11,985 |
Helpers for working with closures that don’t capture any variables.
The fn_item! macro makes a closure with no captures,
and can be accepted into functions with ImplFnItem!.
This is useful for dealing with function pointers in a more composable way.
fn make_fn_ptr<F>(_: ImplFnItem![F: for<'a> Fn(&'a str) -> u32]) -> fn(&str) -> u32
where
F: for<'a> FnItem<(&'a str,), u32>
{
|s| F::call((s,))
}
let fn_ptr = make_fn_ptr(fn_item!(|s| s.parse::<u32>().unwrap()));
assert_eq!(fn_ptr("4115"), 4115);
use fn_item::FnItem;
use fn_item::ImplFnItem;
use fn_item::fn_item;
If you don’t want to add a generic parameter to your outer function, you can use an inner function instead:
fn make_fn_ptr((f, ..): ImplFnItem![for<'a> Fn(&'a str) -> u32]) -> fn(&str) -> u32 {
fn inner<F: for<'a> FnItem<(&'a str,), u32>>(_: F) -> fn(&str) -> u32 {
|s| F::call((s,))
}
inner(f)
}
MIT.