rec_any

Macro rec_any 

Source
macro_rules! rec_any {
    ($in_list:expr, $($func:tt)*) => { ... };
}
Expand description

Helper for writing const fn equivalents of NList::any

This macro acts like a function with this signature:

use nlist::{NList, PeanoInt};

use nlist::receiver::Receiver;

fn rec_any<'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
    ) -> bool

The 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 EVEN_IN_EMPTY: bool = any_even(&nlist![]);
assert!(!EVEN_IN_EMPTY);

const ANY_EVEN: bool = any_even(&nlist![3, 5, 9]);
assert!(!ANY_EVEN);
 
 
const fn any_even<L>(list: &NList<u128, L>) -> bool
where
    L: PeanoInt
{
    nlist::rec_any!{list, |elem: &u128, next| *elem % 2 == 0 || any_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 ANY_ODD: bool = any_odd(nlist![8, 13, 24]);
assert!(ANY_ODD);
 
const fn any_odd<L>(list: NList<u128, L>) -> bool
where
    L: PeanoInt
{
    nlist::rec_any!{list, |elem: u128, next| {
        // works around "destructor cannot be evaluated at compile-time" error
        let next = next.assert_copy();

        elem % 2 == 1 || any_odd(MD::into_inner(next))
    }}

}