image/
io.rs

1//! Input and output of images.
2
3use std::io;
4use std::io::Read as _;
5
6/// The decoder traits.
7pub(crate) mod decoder;
8/// The encoder traits.
9pub(crate) mod encoder;
10
11pub(crate) mod format;
12pub(crate) mod free_functions;
13pub(crate) mod image_reader_type;
14pub(crate) mod limits;
15
16#[deprecated(note = "this type has been moved and renamed to image::ImageReader")]
17/// Deprecated re-export of `ImageReader` as `Reader`
18pub type Reader<R> = ImageReader<R>;
19#[deprecated(note = "this type has been moved to image::Limits")]
20/// Deprecated re-export of `Limits`
21pub type Limits = limits::Limits;
22#[deprecated(note = "this type has been moved to image::LimitSupport")]
23/// Deprecated re-export of `LimitSupport`
24pub type LimitSupport = limits::LimitSupport;
25
26pub(crate) use self::image_reader_type::ImageReader;
27
28/// Adds `read_exact_vec`
29pub(crate) trait ReadExt {
30    fn read_exact_vec(&mut self, vec: &mut Vec<u8>, len: usize) -> io::Result<()>;
31}
32
33impl<R: io::Read> ReadExt for R {
34    fn read_exact_vec(&mut self, vec: &mut Vec<u8>, len: usize) -> io::Result<()> {
35        let initial_len = vec.len();
36        vec.try_reserve(len)?;
37        match self.take(len as u64).read_to_end(vec) {
38            Ok(read) if read == len => Ok(()),
39            fail => {
40                vec.truncate(initial_len);
41                Err(fail.err().unwrap_or(io::ErrorKind::UnexpectedEof.into()))
42            }
43        }
44    }
45}