scuffle_bytes_util/
io_ext.rs

1use std::io;
2
3/// Adds helfpful utilities for working with [`io::Result`].
4pub trait IoResultExt<T> {
5    /// Converts an [`io::ErrorKind::UnexpectedEof`] error into a [`None`] value.
6    fn eof_to_none(self) -> Result<Option<T>, io::Error>;
7}
8
9impl<T> IoResultExt<T> for io::Result<T> {
10    fn eof_to_none(self) -> Result<Option<T>, io::Error> {
11        match self {
12            Ok(value) => Ok(Some(value)),
13            Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => Ok(None),
14            Err(e) => Err(e),
15        }
16    }
17}