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
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors
//

use crate::Task;
use std::io::Read;

///
/// A Reader Task takes a Read, and updates the task with the bytes as they're read.
pub struct ReaderTask<T: Read> {
    reader: T,
    task: Task,
}

impl<T: Read> ReaderTask<T> {
    /// Creates a new reader task from the specified reader, and task
    #[must_use]
    pub fn new(reader: T, task: Task) -> Self {
        ReaderTask { reader, task }
    }
}

impl<T: Read> Drop for ReaderTask<T> {
    fn drop(&mut self) {
        self.task.mark_all_completed();
    }
}
impl<T: Read> Read for ReaderTask<T> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let read = self.reader.read(buf)?;
        self.task.mark_some_completed(read as u64);
        Ok(read)
    }
}