nod/disc/
streams.rs

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
//! Partition file read stream.

use std::{
    io,
    io::{BufRead, Read, Seek, SeekFrom},
};

use super::PartitionBase;

/// A file read stream borrowing a [`PartitionBase`].
pub type FileStream<'a> = WindowedStream<&'a mut dyn PartitionBase>;

/// A file read stream owning a [`PartitionBase`].
pub type OwnedFileStream = WindowedStream<Box<dyn PartitionBase>>;

/// A read stream with a fixed window.
#[derive(Clone)]
pub struct WindowedStream<T>
where T: BufRead + Seek
{
    base: T,
    pos: u64,
    begin: u64,
    end: u64,
}

impl<T> WindowedStream<T>
where T: BufRead + Seek
{
    /// Creates a new windowed stream with offset and size.
    ///
    /// Seeks underlying stream immediately.
    #[inline]
    pub fn new(mut base: T, offset: u64, size: u64) -> io::Result<Self> {
        base.seek(SeekFrom::Start(offset))?;
        Ok(Self { base, pos: offset, begin: offset, end: offset + size })
    }

    /// Returns the length of the window.
    #[inline]
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> u64 { self.end - self.begin }
}

impl<T> Read for WindowedStream<T>
where T: BufRead + Seek
{
    #[inline]
    fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
        let buf = self.fill_buf()?;
        let len = buf.len().min(out.len());
        out[..len].copy_from_slice(&buf[..len]);
        self.consume(len);
        Ok(len)
    }
}

impl<T> BufRead for WindowedStream<T>
where T: BufRead + Seek
{
    #[inline]
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        let limit = self.end.saturating_sub(self.pos);
        if limit == 0 {
            return Ok(&[]);
        }
        let buf = self.base.fill_buf()?;
        let max = (buf.len() as u64).min(limit) as usize;
        Ok(&buf[..max])
    }

    #[inline]
    fn consume(&mut self, amt: usize) {
        self.base.consume(amt);
        self.pos += amt as u64;
    }
}

impl<T> Seek for WindowedStream<T>
where T: BufRead + Seek
{
    #[inline]
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        let mut pos = match pos {
            SeekFrom::Start(p) => self.begin + p,
            SeekFrom::End(p) => self.end.saturating_add_signed(p),
            SeekFrom::Current(p) => self.pos.saturating_add_signed(p),
        };
        if pos < self.begin {
            pos = self.begin;
        } else if pos > self.end {
            pos = self.end;
        }
        let result = self.base.seek(SeekFrom::Start(pos))?;
        self.pos = result;
        Ok(result - self.begin)
    }

    #[inline]
    fn stream_position(&mut self) -> io::Result<u64> { Ok(self.pos) }
}