macro_rules! rec_from_fn {
($($closure:tt)*) => { ... };
}Expand description
Helper for writing const fn equivalents of NList::from_fn
This macro acts like a function with this signature:
use nlist::{NList, PeanoInt};
fn rec_from_fn<F, T, L>(func: F) -> NList<T, L>
where
L: PeanoInt,
F: FnOnce() -> (T, NList<T, L::SubOneSat>),The closure is called when the produced list is non-empty (i.e.: L != 0).
This doesn’t pass an index to the closure, while NList::from_fn does.
§Alternatives
An alternative for constructing an NList with a closure is to use
konst::array::from_fn_+NList::from_array:
const LIST: NList<T, L> = NList::from_array(konst::array::from_fn_!(some_closure_code));Caveat: this only works for small lengths (those that impl the IntoPeano trait)
§Example
use nlist::{NList, Peano, PeanoInt};
const POWS: NList<u128, Peano!(5)> = powers_of_two();
assert_eq!(POWS.into_array(), [1, 2, 4, 8, 16]);
const fn powers_of_two<L: PeanoInt>() -> NList<u128, L> {
const fn inner<L: PeanoInt>(pow: u32) -> NList<u128, L> {
nlist::rec_from_fn!(|| (1 << pow, inner(pow + 1)))
}
inner(0)
}