Skip to main content

read_exact_counted

Function read_exact_counted 

Source
pub fn read_exact_counted<R: Read + ?Sized>(
    reader: &mut R,
    buffer: &mut [u8],
    codec: CodecKind,
    bytes_processed: usize,
) -> Result<(), CodecError>
Expand description

Fills buffer from reader and tracks progress for codec errors.

codec identifies the codec performing the read. bytes_processed is the number of bytes that codec processed before this buffer; bytes read into buffer are added to that base when an error is reported.

Operations interrupted with io::ErrorKind::Interrupted are retried. If the reader reaches the end of its input before filling buffer, the result is a CodecErrorKind::UnexpectedEof error.

§Example

use mcproto_codec::{
    error::CodecKind,
    io::read_exact_counted,
};

let mut input = [0x12, 0x34].as_slice();
let mut bytes = [0; 2];
read_exact_counted(&mut input, &mut bytes, CodecKind::Short, 0)?;
assert_eq!(bytes, [0x12, 0x34]);

§Errors

Returns a CodecError if the reader reaches an unexpected end of input or reports another I/O error.