Function array_utils::initialize_till [−][src]
pub fn initialize_till<T, F, const OUTPUT_SIZE: usize>(
f: F,
till: T,
fill: T
) -> ([T; OUTPUT_SIZE], usize) where
T: Copy + PartialEq,
F: Fn(usize) -> T,
Initialize a sized array from a closure till a certain value appears.
Generates a new sized array generated from generator closure, which turns a index into a
element of the generated array. If the given till
value is found, the rest of the output
array is filled with the fill
value. Along with the generated array, this utility returns
at what index the given till
value was found (OUTPUT_SIZE
if not found).
Examples
use array_utils::initialize_till; let till_five: ([usize; 8], usize) = initialize_till( |index| index, // Generator closure 5, // Till this value 42 // Fill the rest with ); assert_eq!(till_five, ([0, 1, 2, 3, 4, 42, 42, 42], 5)); // Especially useful for null terminated data streams. fn get_stream_byte() -> u8 { // ...snip } // Fetch stream bytes till it find a `0` byte. Fill the rest with `0` bytes. assert_eq!(initialize_till(|_| get_stream_byte(), 0, 0), ([4, 2, 1, 3, 3, 7, 0, 0, 0], 6));
Panics
Only panics if the given f
panics.