dh/
limited.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
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
use std::io::{Read, Result, Seek, SeekFrom, Write};

use crate::{Readable, Rw, Seekable, Writable};

/// A limited reader.
///
/// See [`Readable::limit`] for more information.
pub struct RLimited<'a, T: Readable<'a> + ?Sized> {
    data: &'a mut T,
    start: u64,
    end: u64,
}

impl<'a, T: Readable<'a>> Read for RLimited<'a, T> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let start_pos = self.data.pos()?;
        let end_pos = start_pos + buf.len() as u64;

        if start_pos < self.start || end_pos > self.end {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "reading out of bounds",
            ));
        }

        self.data.read(buf)
    }
}

impl<'a, T: Readable<'a>> Seek for RLimited<'a, T> {
    fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
        let new_pos = match pos {
            SeekFrom::Start(offset) => self.start + offset,
            SeekFrom::End(offset) => (self.end as i128 + offset as i128) as u64,
            SeekFrom::Current(offset) => {
                let current = self.data.pos()?;
                (current as i128 + offset as i128) as u64
            }
        };

        if new_pos < self.start || new_pos > self.end {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "new position is out of bounds",
            ));
        }

        Ok(self.data.to(new_pos)? - self.start)
    }
}

impl<'a, T: Readable<'a>> Seekable for RLimited<'a, T> {}

impl<'a, T: Readable<'a>> Readable<'a> for RLimited<'a, T> {
    fn lock(&mut self, block: bool) -> Result<()> {
        self.data.lock(block)
    }

    fn unlock(&mut self) -> Result<()> {
        self.data.unlock()
    }

    fn close(self) -> Result<Option<crate::DataType<'a>>> {
        Ok(None)
    }
}

pub(crate) fn limit_r<'a, T: Readable<'a> + ?Sized>(
    data: &'a mut T,
    start: u64,
    length: u64,
) -> Result<RLimited<'a, T>> {
    data.to(start)?;
    Ok(RLimited {
        data,
        start,
        end: start + length,
    })
}

/// A limited writer.
///
/// See [`Writable::limit`] for more information.
pub struct WLimited<'a, T: Writable<'a> + ?Sized> {
    data: &'a mut T,
    start: u64,
    end: u64,
}

impl<'a, T: Writable<'a>> Write for WLimited<'a, T> {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        let start_pos = self.data.pos()?;
        let end_pos = start_pos + buf.len() as u64;

        if start_pos < self.start || end_pos > self.end {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "writing out of bounds",
            ));
        }

        self.data.write(buf)
    }

    fn flush(&mut self) -> Result<()> {
        self.data.flush()
    }
}

impl<'a, T: Writable<'a>> Seek for WLimited<'a, T> {
    fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
        let new_pos = match pos {
            SeekFrom::Start(offset) => self.start + offset,
            SeekFrom::End(offset) => (self.end as i128 + offset as i128) as u64,
            SeekFrom::Current(offset) => {
                let current = self.data.pos()?;
                (current as i128 + offset as i128) as u64
            }
        };

        if new_pos < self.start || new_pos > self.end {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "new position is out of bounds",
            ));
        }

        Ok(self.data.to(new_pos)? - self.start)
    }
}

impl<'a, T: Writable<'a>> Seekable for WLimited<'a, T> {}

impl<'a, T: Writable<'a>> Writable<'a> for WLimited<'a, T> {
    fn alloc(&mut self, _: u64) -> Result<()> {
        Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            "alloc is not supported for limited writers",
        ))
    }

    fn lock(&mut self, block: bool) -> Result<()> {
        self.data.lock(block)
    }

    fn unlock(&mut self) -> Result<()> {
        self.data.unlock()
    }

    fn close(self) -> Result<Option<crate::DataType<'a>>> {
        Ok(None)
    }
}

pub(crate) fn limit_w<'a, T: Writable<'a> + ?Sized>(
    data: &'a mut T,
    start: u64,
    length: u64,
) -> Result<WLimited<'a, T>> {
    data.to(start)?;
    Ok(WLimited {
        data,
        start,
        end: start + length,
    })
}

/// A limited R/W stream.
///
/// See [`Rw::rw_limit`] for more information.
pub struct RwLimited<'a, T: Rw<'a> + ?Sized> {
    data: &'a mut T,
    start: u64,
    end: u64,
}

impl<'a, T: Rw<'a>> Read for RwLimited<'a, T> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let start_pos = self.data.pos()?;
        let end_pos = start_pos + buf.len() as u64;

        if start_pos < self.start || end_pos > self.end {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "reading out of bounds",
            ));
        }

        self.data.read(buf)
    }
}

impl<'a, T: Rw<'a>> Write for RwLimited<'a, T> {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        let start_pos = self.data.pos()?;
        let end_pos = start_pos + buf.len() as u64;

        if start_pos < self.start || end_pos > self.end {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "writing out of bounds",
            ));
        }

        self.data.write(buf)
    }

    fn flush(&mut self) -> Result<()> {
        self.data.flush()
    }
}

impl<'a, T: Rw<'a>> Seek for RwLimited<'a, T> {
    fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
        let new_pos = match pos {
            SeekFrom::Start(offset) => self.start + offset,
            SeekFrom::End(offset) => (self.end as i128 + offset as i128) as u64,
            SeekFrom::Current(offset) => {
                let current = self.data.pos()?;
                (current as i128 + offset as i128) as u64
            }
        };

        if new_pos < self.start || new_pos > self.end {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "new position is out of bounds",
            ));
        }

        Ok(self.data.to(new_pos)? - self.start)
    }
}

impl<'a, T: Rw<'a>> Seekable for RwLimited<'a, T> {}

impl<'a, T: Rw<'a>> Readable<'a> for RwLimited<'a, T> {
    fn lock(&mut self, block: bool) -> Result<()> {
        Readable::lock(self.data, block)
    }

    fn unlock(&mut self) -> Result<()> {
        Readable::unlock(self.data)
    }

    fn close(self) -> Result<Option<crate::DataType<'a>>> {
        Ok(None)
    }
}

impl<'a, T: Rw<'a>> Writable<'a> for RwLimited<'a, T> {
    fn alloc(&mut self, _: u64) -> Result<()> {
        Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            "alloc is not supported for limited writers",
        ))
    }

    fn lock(&mut self, block: bool) -> Result<()> {
        Writable::lock(self.data, block)
    }

    fn unlock(&mut self) -> Result<()> {
        Writable::unlock(self.data)
    }

    fn close(self) -> Result<Option<crate::DataType<'a>>> {
        Ok(None)
    }
}

impl<'a, T: Rw<'a>> Rw<'a> for RwLimited<'a, T> {
    fn rw_close(self) -> Result<Option<crate::DataType<'a>>> {
        Ok(None)
    }
}

pub(crate) fn limit_rw<'a, T: Rw<'a> + ?Sized>(
    data: &'a mut T,
    start: u64,
    length: u64,
) -> Result<RwLimited<'a, T>> {
    data.to(start)?;
    Ok(RwLimited {
        data,
        start,
        end: start + length,
    })
}