Macro seq::seqdef_try [] [src]

macro_rules! seqdef_try {
    ($id:ident, $typ:ty, $max:expr; $rt:expr => $iterator:expr ) => { ... };
}

The macro seqdef_try! places values from iterator as sequence on stack.

This macro reserves MAX elements on the stack every time when entering the function context. The upper limit MAX is defined at compile time. At runtime the sequence is constructed, reading the elements from the iterator and placing them in the reserved stack-memory. When the function context is left, the stack-memory is released. The macro seqdef_try! will declare the specified identifier as type Result>. Rolling out the values from iterator into the reserved stack may fail if the iterator is empty, or if the amount exceeds MAX. The value of x must be checked after construction with seqdef_try!.

Note! No matter the number of elements returned by the iterator, the macro is always reserving stack-memory for MAX elements. If you choose too large, the stack might run out of memory. The iterator provided to the macro may consume the underlying container-elements or clone each element.

Example use std::ptr;

fn large_seq_rollout_on_stack() { const MAX: usize = 2000; let large_list: &[i32] = &[42; MAX]; // define x of type Result>, read all elements from array and place them on stack as sequence seqdef_try!(x, i32, MAX; empty() => large_list.iter()); // handling the result, Ok or Error match &x { &Ok(ref sequence) => println!("large sum {}", (&*sequence).into_iter().fold(0i32, ops::Add::add)), &Err(reason) => println!("roll out failed due to {}", reason) } } ```