fn read_next_mail(
        buf: &mut Vec<u8>,
        parsing_state: &mut ParsingState,
    ) -> Result<Option<Vec<u8>>> {
        let mut email_contents: Vec<u8> = vec![];
        let mut separator_idx = 0;

        loop {
            if parsing_state.bytes_left == 0 {
                return Ok(None);
            }
            let cur_buf = Email::read_into_buffer(buf, parsing_state)?;

            for i in 0..cur_buf.len() {
                let cur = cur_buf[i];
                let byte_matches = cur == SEPARATOR_BYTES[separator_idx];
                let mut matches = false;
                if byte_matches && separator_idx == SEPARATOR_BYTES.len() - 1 {
                    // matching in the middle of the file.
                    // not interested in the extra \n so I take only [0..i]
                    matches = true;
                    email_contents.extend(cur_buf[0..i].iter());
                } else if separator_idx == SEPARATOR_BYTES.len() - 2
                    && parsing_state.bytes_left as usize - (i + 1) == 0
                {
                    // hit the beginning of the file (bytes_left - (i+1) == 0)
                    // => we don't require the leading \n from the separator bytes
                    // do collect the current letter too [0..(i+1)]
                    matches = true;
                    email_contents.extend(cur_buf[0..=i].iter());
                }
                if matches {
                    // found the marker for the beginning of the email
                    email_contents.reverse();
                    parsing_state.bytes_left -= (i + 1) as u64;
                    parsing_state
                        .reader
                        .seek(SeekFrom::Start(parsing_state.bytes_left))?;
                    return Ok(Some(email_contents));
                }
                if byte_matches {
                    separator_idx += 1;
                } else {
                    separator_idx = 0;
                }
            }
            email_contents.extend(cur_buf.iter());
            parsing_state.bytes_left -= cur_buf.len() as u64;
        }
    }
// re-reading the buffer from the file for each new email, but i rely on the bufreader too
re-reading the buffer from the file for each new email, but i rely on the bufreader too
re - reading the buffer from the file for each new email but i rely on the bufreader too
fn read_next_mail(
        buf: &mut Vec<u8>,
        parsing_state: &mut ParsingState,
    ) -> Result<Option<Vec<u8>>> {
        let mut email_contents: Vec<u8> = vec![];
        let mut separator_idx = 0;
        loop {
            if parsing_state.bytes_left == 0 {
                return Ok(None);
            }
            let cur_buf = Email::read_into_buffer(buf, parsing_state)?;
            for i in 0..cur_buf.len() {
                let cur = cur_buf[i];
                let byte_matches = cur == SEPARATOR_BYTES[separator_idx];
                let mut matches = false;
                if byte_matches && separator_idx == SEPARATOR_BYTES.len() - 1 {
                    // matching in the middle of the file.
                    // not interested in the extra \n so I take only [0..i]
                    matches = true;
                    email_contents.extend(cur_buf[0..i].iter());
                } else if separator_idx == SEPARATOR_BYTES.len() - 2
                    && parsing_state.bytes_left as usize - (i + 1) == 0
                {
                    // hit the beginning of the file (bytes_left - (i+1) == 0)
                    // => we don't require the leading \n from the separator bytes
                    // do collect the current letter too [0..(i+1)]
                    matches = true;
                    email_contents.extend(cur_buf[0..=i].iter());
                }
                if matches {
                    // found the marker for the beginning of the email
                    email_contents.reverse();
                    parsing_state.bytes_left -= (i + 1) as u64;
                    parsing_state
                        .reader
                        .seek(SeekFrom::Start(parsing_state.bytes_left))?;
                    return Ok(Some(email_contents));
                }
                if byte_matches {
                    separator_idx += 1;
                } else {
                    separator_idx = 0;
                }
            }
            email_contents.extend(cur_buf.iter());
            parsing_state.bytes_left -= cur_buf.len() as u64;
        }
    }
fn read_next_mail ( buf : & mut Vec < u8 > , parsing_state : & mut ParsingState , ) -> Result < Option < Vec < u8 > > > { let mut email_contents : Vec < u8 > = vec ! [ ] ; let mut separator_idx = 0 ; loop { if parsing_state . bytes_left == 0 { return Ok ( None ) ; } let cur_buf = Email :: read_into_buffer ( buf , parsing_state ) ? ; for i in 0 .. cur_buf . len ( ) { let cur = cur_buf [ i ] ; let byte_matches = cur == SEPARATOR_BYTES [ separator_idx ] ; let mut matches = false ; if byte_matches && separator_idx == SEPARATOR_BYTES . len ( ) - 1 { // matching in the middle of the file. // not interested in the extra \n so I take only [0..i] matches = true ; email_contents . extend ( cur_buf [ 0 .. i ] . iter ( ) ) ; } else if separator_idx == SEPARATOR_BYTES . len ( ) - 2 && parsing_state . bytes_left as usize - ( i + 1 ) == 0 { // hit the beginning of the file (bytes_left - (i+1) == 0) // => we don't require the leading \n from the separator bytes // do collect the current letter too [0..(i+1)] matches = true ; email_contents . extend ( cur_buf [ 0 ..= i ] . iter ( ) ) ; } if matches { // found the marker for the beginning of the email email_contents . reverse ( ) ; parsing_state . bytes_left -= ( i + 1 ) as u64 ; parsing_state . reader . seek ( SeekFrom :: Start ( parsing_state . bytes_left ) ) ? ; return Ok ( Some ( email_contents ) ) ; } if byte_matches { separator_idx += 1 ; } else { separator_idx = 0 ; } } email_contents . extend ( cur_buf . iter ( ) ) ; parsing_state . bytes_left -= cur_buf . len ( ) as u64 ; } }
re-reading the buffer from the file for each new email, but i rely on the bufreader too
re - reading the buffer from the file for each new email but i rely on the bufreader too
buf
&mut Vec<u8>
parsing_state
&mut ParsingState
buf
&mut Vec<u8>
parsing_state
&mut ParsingState