[][src]Function cookie_factory::combinator::back_to_the_buffer

pub fn back_to_the_buffer<W: BackToTheBuffer, Tmp, Gen, Before>(
    reserved: usize,
    gen: Gen,
    before: Before
) -> impl SerializeFn<W> where
    Gen: Fn(WriteContext<W>) -> Result<(WriteContext<W>, Tmp), GenError>,
    Before: Fn(WriteContext<W>, Tmp) -> GenResult<W>, 

Reserves space for the Before combinator, applies the Gen combinator, then applies the Before combinator with the output from Gen onto the reserved space.

use cookie_factory::{gen, gen_simple, sequence::tuple, combinator::{back_to_the_buffer, string}, bytes::be_u8, bytes::be_u32};

let mut buf = [0; 9];
gen_simple(tuple((
    back_to_the_buffer(
        4,
        move |buf| gen(string("test"), buf),
        move |buf, len| gen_simple(be_u32(len as u32), buf)
    ),
    be_u8(42)
)), &mut buf[..]).unwrap();
assert_eq!(&buf, &[0, 0, 0, 4, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, 42]);