/// Begins an asynchronous read of a message from `reader`.
Begins an asynchronous read of a message from `reader`.
Begins an asynchronous read of a message from ` reader ` .
pub async fn read_message<R>(mut reader: R, options: message::ReaderOptions) -> Result<Option<message::Reader<OwnedSegments>>>
    where R: AsyncBufRead + Unpin
{
    let mut buf: [u8; 8] = [0; 8];
    {
        let n = packed_read(&mut reader, &mut buf[..]).await?;
        if n == 0 {
            return Ok(None);
        } else if n < 8 {
            packed_read_exact(&mut reader, &mut buf[n..]).await?;
        }
    }
    let (segment_count, first_segment_length) = parse_segment_table_first(&buf[..])?;
    let mut segment_slices: Vec<(usize, usize)> = Vec::with_capacity(segment_count);
    segment_slices.push((0,first_segment_length));
    let mut total_words = first_segment_length;
    if segment_count > 1 {
        if segment_count < 4 {
            // small enough that we can reuse our existing buffer
            packed_read_exact(&mut reader, &mut buf).await?;
            for idx in 0..(segment_count - 1) {
                let segment_len =
                    u32::from_le_bytes(buf[(idx * 4)..(idx + 1) * 4].try_into().unwrap()) as usize;
                segment_slices.push((total_words, total_words + segment_len));
                total_words += segment_len;
            }
        } else {
            let mut segment_sizes = vec![0u8; (segment_count & !1) * 4];
            packed_read_exact(&mut reader, &mut segment_sizes[..]).await?;
            for idx in 0..(segment_count - 1) {
                let segment_len =
                    u32::from_le_bytes(segment_sizes[(idx * 4)..(idx + 1) * 4].try_into().unwrap()) as usize;
                segment_slices.push((total_words, total_words + segment_len));
                total_words += segment_len;
            }
        }
    }
    // Don't accept a message which the receiver couldn't possibly traverse without hitting the
    // traversal limit. Without this check, a malicious client could transmit a very large segment
    // size to make the receiver allocate excessive space and possibly crash.
    if total_words as u64 > options.traversal_limit_in_words  {
        return Err(Error::failed(
            format!("Message has {} words, which is too large. To increase the limit on the \
             receiving end, see capnp::message::ReaderOptions.", total_words)))
    }
    let mut owned_space: Vec<Word> = Word::allocate_zeroed_vec(total_words);
    packed_read_exact(&mut reader, Word::words_to_bytes_mut(&mut owned_space[..])).await?;
    let segments = OwnedSegments {segment_slices: segment_slices, owned_space: owned_space};
    Ok(Some(message::Reader::new(segments, options)))
}
pub async fn read_message < R > ( mut reader : R , options : message :: ReaderOptions ) -> Result < Option < message :: Reader < OwnedSegments > > > where R : AsyncBufRead + Unpin { let mut buf : [ u8 ; 8 ] = [ 0 ; 8 ] ; { let n = packed_read ( & mut reader , & mut buf [ .. ] ) . await ? ; if n == 0 { return Ok ( None ) ; } else if n < 8 { packed_read_exact ( & mut reader , & mut buf [ n .. ] ) . await ? ; } } let ( segment_count , first_segment_length ) = parse_segment_table_first ( & buf [ .. ] ) ? ; let mut segment_slices : Vec < ( usize , usize ) > = Vec :: with_capacity ( segment_count ) ; segment_slices . push ( ( 0 , first_segment_length ) ) ; let mut total_words = first_segment_length ; if segment_count > 1 { if segment_count < 4 { // small enough that we can reuse our existing buffer packed_read_exact ( & mut reader , & mut buf ) . await ? ; for idx in 0 .. ( segment_count - 1 ) { let segment_len = u32 :: from_le_bytes ( buf [ ( idx * 4 ) .. ( idx + 1 ) * 4 ] . try_into ( ) . unwrap ( ) ) as usize ; segment_slices . push ( ( total_words , total_words + segment_len ) ) ; total_words += segment_len ; } } else { let mut segment_sizes = vec ! [ 0u8 ( segment_count 1 ) 4 ] ; packed_read_exact ( & mut reader , & mut segment_sizes [ .. ] ) . await ? ; for idx in 0 .. ( segment_count - 1 ) { let segment_len = u32 :: from_le_bytes ( segment_sizes [ ( idx * 4 ) .. ( idx + 1 ) * 4 ] . try_into ( ) . unwrap ( ) ) as usize ; segment_slices . push ( ( total_words , total_words + segment_len ) ) ; total_words += segment_len ; } } } // Don't accept a message which the receiver couldn't possibly traverse without hitting the // traversal limit. Without this check, a malicious client could transmit a very large segment // size to make the receiver allocate excessive space and possibly crash. if total_words as u64 > options . traversal_limit_in_words { return Err ( Error :: failed ( format ! ( " \
 " total_words ) ) ) } let mut owned_space : Vec < Word > = Word :: allocate_zeroed_vec ( total_words ) ; packed_read_exact ( & mut reader , Word :: words_to_bytes_mut ( & mut owned_space [ .. ] ) ) . await ? ; let segments = OwnedSegments { segment_slices : segment_slices , owned_space : owned_space } ; Ok ( Some ( message :: Reader :: new ( segments , options ) ) ) }
Begins an asynchronous read of a message from `reader`.
Begins an asynchronous read of a message from ` reader ` .
reader
R
options
message::ReaderOptions