read_chunk_iter 0.2.0

Iterator adapters over a reader that yield fixed-size chunks at a time.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::io::Error as IOError;
use std::io::Result as IOResult;
use std::io::{ErrorKind, Read, Seek};

use std::num::NonZeroUsize;

use crate::vectored_read::{read_vectored_into_buf, resolve_read_vectored, VectoredReadSelect};

/// An iterator adapter for readers that yields chunks of bytes in a `Box<[u8]>`.
///
#[derive(Debug)]
pub struct ChunkedReaderIter<R> {
    reader: R,
    reader_vectored: VectoredReadSelect,
    chunk_size: NonZeroUsize,
    buf_size: NonZeroUsize,
    buf: Vec<u8>,
    undrained_byte_count: usize,
    io_error_stash: Option<IOError>,
}
impl<R> ChunkedReaderIter<R> {
    /// Instantiates a new [`ChunkedReaderIter`] that tries to read up to `buf_size` bytes at a time and that yields `chunk_size` bytes as an iterator until reaching EOF.
    /// For readers that implement `Seek`, [`Self::new_with_rewind`] rewinds the given reader.
    ///
    /// # Panics
    /// Panics if `buf_size` is smaller than `chunk_size`.
    pub fn new(
        reader: R,
        chunk_size: NonZeroUsize,
        buf_size: NonZeroUsize,
        reader_vectored: VectoredReadSelect,
    ) -> Self {
        assert!(buf_size >= chunk_size);
        Self {
            reader,
            reader_vectored,
            chunk_size,
            buf_size,
            buf: Vec::with_capacity(buf_size.into()),
            undrained_byte_count: 0,
            io_error_stash: None,
        }
    }

    /// Returns the wrapped reader and any unyielded data. This includes both
    /// any IOError that occured on the last read attempt and unyielded data
    /// that was read before then.
    #[inline]
    pub fn into_inner(self) -> (Box<[u8]>, Option<IOError>, R) {
        (
            self.buf[self.undrained_byte_count..]
                .iter()
                .copied()
                .collect(),
            self.io_error_stash,
            self.reader,
        )
    }
    /// Returns the chunk size which is yielded by the iterator.
    #[inline]
    pub fn chunk_size(&self) -> NonZeroUsize {
        self.chunk_size
    }
    /// Returns the size of the buffer used to read from the underlying reader.
    #[inline]
    pub fn buf_size(&self) -> NonZeroUsize {
        self.buf_size
    }
    /// Returns whether the reads will be vectored or not.
    #[inline]
    pub fn vectored_read_select(&self) -> VectoredReadSelect {
        self.reader_vectored
    }
    /// Returns a slice of the internal buffer used to buffer reads. The slice only contains valid buffered data, so it will be smaller than the value returned by [`Self::buf_size`].
    #[inline]
    pub fn buf(&self) -> &[u8] {
        &self.buf[self.undrained_byte_count..]
    }
}
impl<R: Seek> ChunkedReaderIter<R> {
    /// Constructs a new [`ChunkedReaderIter`] that rewinds the reader to ensure that all data is yielded by the iterator.
    /// See [`ChunkedReaderIter::new`] for descriptions of the other parameters.
    pub fn new_with_rewind(
        mut reader: R,
        chunk_size: NonZeroUsize,
        buf_size: NonZeroUsize,
        reader_vectored: VectoredReadSelect,
    ) -> Self {
        reader.rewind().unwrap();
        Self::new(reader, chunk_size, buf_size, reader_vectored)
    }
}

impl<R: Read> Iterator for ChunkedReaderIter<R> {
    type Item = IOResult<Box<[u8]>>;

    /// Yields `self.chunk_size` bytes at a time until reaching EOF, after which it yields the remaining bytes before returning `None`.
    /// All bytes successfully read are eventually returned: if reads into the buffer result in an error, the previously read data is yielded first, and then the error is passed up.
    ///
    /// Note: If reading from a readable object that is being concurrently modified (e.g. a file that is being appended to by another process),
    /// EOF may be hit more than once, resulting in more chunks after a chunk smaller than `self.chunk_size` or more chunks after yielding `None`.
    /// (This is also a concern with the base [`Read`] trait, which may return more data even after returning `Ok(0)`).
    fn next(&mut self) -> Option<Self::Item> {
        if self.io_error_stash.is_some() {
            let err_obj = self.io_error_stash.take().unwrap();
            return Some(Err(err_obj));
        }
        let mut read_offset = self.buf.len();
        assert!(self.undrained_byte_count <= read_offset);
        // Temporarily resize Vec to try to fill it
        // Due to initialization with `with_capacity` we do not need to reallocate
        self.buf.resize(self.buf_size.into(), 0x00);
        // Try to fill entire buf, but we're good if we have a whole chunk
        while read_offset < self.chunk_size.into() {
            let reader_result = match resolve_read_vectored(&self.reader, self.reader_vectored) {
                true => read_vectored_into_buf(
                    &mut self.reader,
                    &mut self.buf[read_offset..],
                    self.chunk_size,
                ),
                false => self.reader.read(&mut self.buf[read_offset..]),
            };
            match reader_result {
                Ok(0) => {
                    break;
                }
                Ok(n) => {
                    read_offset += n;
                }
                Err(e) if e.kind() == ErrorKind::Interrupted => { /* continue */ }
                Err(e) => {
                    // Shrink Vec back to how much was actually read
                    self.buf.truncate(read_offset);
                    // Yield currently read data before yielding Err
                    // We are in loop so read_offset < chunk_size
                    if read_offset > 0 {
                        assert!(self.io_error_stash.is_none());
                        self.io_error_stash = Some(e);
                        let boxed_data: Box<[u8]> =
                            self.buf.drain(self.undrained_byte_count..).collect();
                        self.undrained_byte_count = 0;
                        return Some(Ok(boxed_data));
                    }
                    return Some(Err(e));
                }
            }
        }
        // How much data in the buffer has not been yielded yet?
        let unyielded_count = read_offset - self.undrained_byte_count;
        if unyielded_count == 0 {
            // We hit EOF and ran out of buffer contents
            self.buf.clear();
            self.undrained_byte_count = 0;
            return None;
        }
        // We just checked that this is nonzero
        let unyielded_count = NonZeroUsize::new(unyielded_count).unwrap();
        // Shrink Vec back to how much was actually read
        self.buf.truncate(read_offset);

        // We keep stale, already-yielded data and don't memmove every time
        // In order to reduce the performance penalty of such memory accesses
        if self.chunk_size > unyielded_count {
            // Yield the remaining data at EOF
            let boxed_data: Box<[u8]> = self.buf.drain(self.undrained_byte_count..).collect();
            self.buf.clear();
            self.undrained_byte_count = 0;
            Some(Ok(boxed_data))
        } else {
            let ret_buf = self.buf[self.undrained_byte_count
                ..self.undrained_byte_count + usize::from(self.chunk_size)]
                .iter()
                .copied()
                .collect();
            self.undrained_byte_count += usize::from(self.chunk_size);
            assert!(read_offset >= self.undrained_byte_count);
            if read_offset - self.undrained_byte_count < self.chunk_size.into() {
                self.buf.drain(..self.undrained_byte_count);
                self.undrained_byte_count = 0;
            }
            Some(Ok(ret_buf))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::io::Cursor;

    use crate::dev_helpers::{FunnyRead, IceCubeRead, TruncatedRead};

    #[test]
    fn chunked_read_iter_funnyread() {
        let funny_read = FunnyRead::default();
        let mut funny_read_iter = ChunkedReaderIter::new(
            funny_read,
            NonZeroUsize::new(4).unwrap(),
            NonZeroUsize::new(5).unwrap(),
            VectoredReadSelect::Yes,
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[0, 1, 2, 3]
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[4, 5, 6, 7]
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[8, 9, 10, 11]
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[12, 13, 14, 15]
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[16, 17, 18, 19]
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[20, 21, 22, 23]
        );
    }
    #[test]
    fn chunked_read_iter_icecuberead() {
        let funny_read = IceCubeRead::default();
        let mut funny_read_iter = ChunkedReaderIter::new(
            funny_read,
            NonZeroUsize::new(2).unwrap(),
            NonZeroUsize::new(5).unwrap(),
            VectoredReadSelect::No,
        );
        assert_eq!(funny_read_iter.next().unwrap().unwrap().as_ref(), &[9, 99]);
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[0x99, 9]
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            &[99, 0x99]
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap_err().kind(),
            ErrorKind::Other
        );
        assert!(funny_read_iter.next().is_none());
        assert_eq!(funny_read_iter.next().unwrap().unwrap().as_ref(), &[9, 99]);
    }
    #[test]
    fn chunked_read_iter_truncatedread() {
        let funny_read = TruncatedRead::default();
        let mut funny_read_iter = ChunkedReaderIter::new(
            funny_read,
            NonZeroUsize::new(3).unwrap(),
            NonZeroUsize::new(3).unwrap(),
            VectoredReadSelect::No,
        );
        assert_eq!(funny_read_iter.next().unwrap().unwrap().as_ref(), b"rei");
        assert_eq!(funny_read_iter.next().unwrap().unwrap().as_ref(), b"mu");
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap_err().kind(),
            ErrorKind::Other
        );
        assert!(funny_read_iter.next().is_none());
        assert_eq!(funny_read_iter.next().unwrap().unwrap().as_ref(), b"rei");
    }
    #[test]
    fn chunked_read_iter_truncatedread_large() {
        let funny_read = TruncatedRead::default();
        let mut funny_read_iter = ChunkedReaderIter::new(
            funny_read,
            NonZeroUsize::new(11).unwrap(),
            NonZeroUsize::new(22).unwrap(),
            VectoredReadSelect::No,
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            b"reimureimu"
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap_err().kind(),
            ErrorKind::Other
        );
        assert!(funny_read_iter.next().is_none());
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            b"reimureimu"
        );
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap_err().kind(),
            ErrorKind::Other
        );
        assert!(funny_read_iter.next().is_none());
        assert_eq!(
            funny_read_iter.next().unwrap().unwrap().as_ref(),
            b"reimureimu"
        );
    }

    #[test]
    fn chunked_read_iter_cursor_large() {
        let data_buf = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        let data_cursor = Cursor::new(data_buf);
        let mut data_chunk_iter = ChunkedReaderIter::new(
            data_cursor,
            NonZeroUsize::new(4).unwrap(),
            NonZeroUsize::new(8).unwrap(),
            VectoredReadSelect::No,
        );
        assert_eq!(
            data_chunk_iter.next().unwrap().unwrap().as_ref(),
            &[1, 2, 3, 4]
        );
        assert_eq!(
            data_chunk_iter.next().unwrap().unwrap().as_ref(),
            &[5, 6, 7, 8]
        );
        assert_eq!(data_chunk_iter.next().unwrap().unwrap().as_ref(), &[9]);
        assert!(data_chunk_iter.next().is_none());

        let (unyielded_data, unyielded_error, _) = data_chunk_iter.into_inner();
        assert_eq!(unyielded_data.as_ref(), &[]);
        assert!(unyielded_error.is_none());
    }
    #[test]
    fn chunked_read_iter_cursor_large_into_inner() {
        let data_buf = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        let data_cursor = Cursor::new(data_buf);
        let mut data_chunk_iter = ChunkedReaderIter::new(
            data_cursor,
            NonZeroUsize::new(4).unwrap(),
            NonZeroUsize::new(8).unwrap(),
            VectoredReadSelect::No,
        );
        assert_eq!(
            data_chunk_iter.next().unwrap().unwrap().as_ref(),
            &[1, 2, 3, 4]
        );
        let (unyielded_data, unyielded_error, _) = data_chunk_iter.into_inner();
        assert_eq!(unyielded_data.as_ref(), &[5, 6, 7, 8]);
        assert!(unyielded_error.is_none());
    }
    #[test]
    fn chunked_read_iter_cursor_while() {
        let data_buf = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        let data_cursor = Cursor::new(data_buf);

        let data_chunks: Vec<_> = ChunkedReaderIter::new(
            data_cursor,
            NonZeroUsize::new(4).unwrap(),
            NonZeroUsize::new(8).unwrap(),
            VectoredReadSelect::No,
        )
        .collect();
        let data_chunks_as_slice: Vec<&[u8]> = data_chunks
            .iter()
            .map(|r| r.as_ref().unwrap().as_ref())
            .collect();
        let expected_data_chunks: &[&[u8]] = &[&[1, 2, 3, 4], &[5, 6, 7, 8], &[9]];
        assert_eq!(data_chunks_as_slice.as_slice(), expected_data_chunks);
    }
    #[test]
    fn chunked_read_iter_cursor_large_buf_eq_chunk() {
        let data_buf = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        let data_cursor = Cursor::new(data_buf);
        let mut data_chunk_iter = ChunkedReaderIter::new(
            data_cursor,
            NonZeroUsize::new(4).unwrap(),
            NonZeroUsize::new(4).unwrap(),
            VectoredReadSelect::No,
        );
        assert_eq!(
            data_chunk_iter.next().unwrap().unwrap().as_ref(),
            &[1, 2, 3, 4]
        );
        assert_eq!(
            data_chunk_iter.next().unwrap().unwrap().as_ref(),
            &[5, 6, 7, 8]
        );
        assert_eq!(data_chunk_iter.next().unwrap().unwrap().as_ref(), &[9]);
        assert!(data_chunk_iter.next().is_none());
    }
    #[test]
    fn chunked_read_iter_cursor_smol() {
        let data_buf = [1, 2, 3];
        let data_cursor = Cursor::new(data_buf);
        let mut data_chunk_iter = ChunkedReaderIter::new(
            data_cursor,
            NonZeroUsize::new(4).unwrap(),
            NonZeroUsize::new(4).unwrap(),
            VectoredReadSelect::No,
        );
        assert_eq!(
            data_chunk_iter.next().unwrap().unwrap().as_ref(),
            &[1, 2, 3]
        );
        assert!(data_chunk_iter.next().is_none());
    }
}