Trait bczhc_lib::io::TryReadExact
source · pub trait TryReadExact {
// Required method
fn try_read_exact(&mut self, buf: &mut [u8]) -> Result<usize>;
}Required Methods§
sourcefn try_read_exact(&mut self, buf: &mut [u8]) -> Result<usize>
fn try_read_exact(&mut self, buf: &mut [u8]) -> Result<usize>
Read exact data
This function blocks. It reads exact data, and returns bytes it reads. The return value will always be the buffer size until it reaches EOF.
When reaching EOF, the return value will be less than the size of the given buffer, or just zero.
Examples
use std::io::stdin;
use bczhc_lib::io::TryReadExact;
let mut stdin = stdin();
let mut buf = [0_u8; 5];
loop {
let result = stdin.try_read_exact(&mut buf);
match result {
Ok(r) => {
if r == 0 {
// EOF
break;
}
println!("Read: {:?}", &buf[..r]);
}
Err(e) => {
eprintln!("IO error: {}", e);
}
}
}