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
use std::future::Future;
use std::io;

use futures::FutureExt;
use vortex_buffer::Buffer;

use crate::VortexReadAt;

/// An adapter that offsets all reads by a fixed amount.
pub struct OffsetReadAt<R> {
    read: R,
    offset: u64,
}

impl<R> Clone for OffsetReadAt<R>
where
    R: Clone,
{
    fn clone(&self) -> Self {
        Self {
            read: self.read.clone(),
            offset: self.offset,
        }
    }
}

impl<R: VortexReadAt> OffsetReadAt<R> {
    pub fn new(read: R, offset: u64) -> Self {
        Self { read, offset }
    }
}

impl<R: VortexReadAt> VortexReadAt for OffsetReadAt<R> {
    fn read_byte_range(
        &self,
        pos: u64,
        len: u64,
    ) -> impl Future<Output = io::Result<Buffer>> + 'static {
        self.read.read_byte_range(pos + self.offset, len)
    }

    fn performance_hint(&self) -> usize {
        self.read.performance_hint()
    }

    fn size(&self) -> impl Future<Output = io::Result<u64>> + 'static {
        let offset = self.offset;
        self.read.size().map(move |len| len.map(|len| len - offset))
    }
}