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
use super::{ReadAt, WriteAt, Size};
use std::cmp::min;
use std::io::{Result, Error, ErrorKind};
pub struct Slice<I> {
io: I,
offset: u64,
size: Option<u64>,
}
impl<I> Slice<I> {
pub fn new(io: I, offset: u64, size: Option<u64>) -> Self {
Slice {
io: io,
offset: offset,
size: size,
}
}
fn avail(&self, pos: u64, bytes: usize) -> usize {
match self.size {
None => bytes,
Some(size) if pos >= size => 0,
Some(size) => min(bytes as u64, size - pos) as usize,
}
}
}
impl<I> Slice<I>
where I: Size
{
pub fn new_to_end(io: I, offset: u64) -> Result<Self> {
match io.size() {
Ok(Some(size)) => Ok(Self::new(io, offset, Some(size - offset))),
_ => Err(Error::new(ErrorKind::InvalidData, "unknown base size")),
}
}
}
impl<I> ReadAt for Slice<I>
where I: ReadAt
{
fn read_at(&self, pos: u64, buf: &mut [u8]) -> Result<usize> {
let bytes = self.avail(pos, buf.len());
self.io.read_at(pos + self.offset, &mut buf[..bytes])
}
}
impl<I> WriteAt for Slice<I>
where I: WriteAt
{
fn write_at(&mut self, pos: u64, buf: &[u8]) -> Result<usize> {
let bytes = self.avail(pos, buf.len());
self.io.write_at(pos + self.offset, &buf[..bytes])
}
fn flush(&mut self) -> Result<()> {
self.io.flush()
}
}
impl<I> Size for Slice<I> {
fn size(&self) -> Result<Option<u64>> {
Ok(self.size)
}
}