macro_rules! rec_all {
($in_list:expr, $($func:tt)*) => { ... };
}Expand description
Helper for writing const fn equivalents of NList::all
This macro acts like a function with this signature:
use nlist::{NList, PeanoInt};
use nlist::receiver::Receiver;
fn rec_all<'a, P, T, L, F>(list: P , func: F) -> bool
where
P: Receiver<'a, NList<T, L>>,
L: PeanoInt,
T: 'a,
F: FnOnce(
... // parameter types explained below
) -> boolThe closure is only called when the list is non-empty (i.e.: when L != 0).
The closure parameters depend on the value of P:
- If
P == NList<T, L>: the parameters are(T, Nlist<T, L::SubOneSat>) - If
P == &NList<T, L>: the parameters are(&T, &Nlist<T, L::SubOneSat>) - If
P == &mut NList<T, L>: the parameters are(&mut T, &mut Nlist<T, L::SubOneSat>)
§Note
Because iteration over the list might terminate before the list is fully consumed, by-value iteration over non-Copy types does not work in const, and by-value iteration over Copy types requires doing what the by-value example does.
§Example
§By reference
Example that takes an NList by reference
use nlist::{NList, Peano, PeanoInt, nlist};
const ALL_EVEN: bool = all_even(&nlist![3, 5, 8]);
assert!(!ALL_EVEN);
const fn all_even<L>(list: &NList<u128, L>) -> bool
where
L: PeanoInt
{
nlist::rec_all!{list, |elem: &u128, next| *elem % 2 == 0 && all_even(next)}
}§By value
Example that takes an NList of Copy elements by value
use nlist::{NList, Peano, PeanoInt, nlist};
use std::mem::ManuallyDrop as MD;
const ALL_EMPTY_ARE_ODD: bool = all_odd(nlist![]);
assert!(ALL_EMPTY_ARE_ODD);
const ALL_ODD: bool = all_odd(nlist![3, 5, 13]);
assert!(ALL_ODD);
const fn all_odd<L>(list: NList<u128, L>) -> bool
where
L: PeanoInt
{
nlist::rec_all!{list, |elem: u128, next| {
// works around "destructor cannot be evaluated at compile-time" error
let next = next.assert_copy();
elem % 2 == 1 && all_odd(MD::into_inner(next))
}}
}