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
use std::io;
use std::fs::File;
use std::path::Path;
use std::borrow::Cow;
use std::ops::Deref;
use memmap::Mmap;
use owning_ref::OwningHandle;
use errors::Result;
enum ByteViewInner<'bytes> {
Buf(Cow<'bytes, [u8]>),
Mmap(Mmap),
}
pub struct ByteView<'bytes> {
inner: ByteViewInner<'bytes>,
}
impl<'bytes> ByteView<'bytes> {
fn from_cow(cow: Cow<'bytes, [u8]>) -> ByteView<'bytes> {
ByteView {
inner: ByteViewInner::Buf(cow),
}
}
pub fn from_slice(buffer: &'bytes [u8]) -> ByteView<'bytes> {
ByteView::from_cow(Cow::Borrowed(buffer))
}
pub fn from_vec(buffer: Vec<u8>) -> ByteView<'static> {
ByteView::from_cow(Cow::Owned(buffer))
}
pub fn from_reader<R: io::Read>(mut reader: R) -> Result<ByteView<'static>> {
let mut buffer = vec![];
reader.read_to_end(&mut buffer)?;
Ok(ByteView::from_vec(buffer))
}
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ByteView<'static>> {
let file = File::open(path)?;
let inner = match unsafe { Mmap::map(&file) } {
Ok(mmap) => ByteViewInner::Mmap(mmap),
Err(err) => {
if err.kind() == io::ErrorKind::InvalidInput {
ByteViewInner::Buf(Cow::Borrowed(b""))
} else {
return Err(err.into());
}
}
};
Ok(ByteView { inner: inner })
}
#[inline(always)]
fn buffer(&self) -> &[u8] {
match self.inner {
ByteViewInner::Buf(ref buf) => buf,
ByteViewInner::Mmap(ref mmap) => mmap.deref(),
}
}
}
impl<'bytes> Deref for ByteView<'bytes> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.buffer()
}
}
impl<'bytes> AsRef<[u8]> for ByteView<'bytes> {
fn as_ref(&self) -> &[u8] {
self.buffer()
}
}
pub struct ByteViewHandle<'bytes, T> {
inner: OwningHandle<Box<ByteView<'bytes>>, Box<(&'bytes [u8], T)>>,
}
impl<'bytes, T> ByteViewHandle<'bytes, T> {
pub fn from_byteview<F>(view: ByteView<'bytes>, f: F) -> Result<ByteViewHandle<'bytes, T>>
where
F: FnOnce(&'bytes [u8]) -> Result<T>,
{
Ok(ByteViewHandle {
inner: OwningHandle::try_new(Box::new(view), |bv| -> Result<_> {
let bytes: &[u8] = unsafe { &*bv };
Ok(Box::new((bytes, f(bytes)?)))
})?,
})
}
pub fn from_slice<F>(buffer: &'bytes [u8], f: F) -> Result<ByteViewHandle<'bytes, T>>
where
F: FnOnce(&'bytes [u8]) -> Result<T>,
{
ByteViewHandle::from_byteview(ByteView::from_slice(buffer), f)
}
pub fn from_vec<F>(vec: Vec<u8>, f: F) -> Result<ByteViewHandle<'static, T>>
where
F: FnOnce(&'static [u8]) -> Result<T>,
{
ByteViewHandle::from_byteview(ByteView::from_vec(vec), f)
}
pub fn from_reader<F, R>(reader: R, f: F) -> Result<ByteViewHandle<'static, T>>
where
F: FnOnce(&'static [u8]) -> Result<T>,
R: io::Read,
{
ByteViewHandle::from_byteview(ByteView::from_reader(reader)?, f)
}
pub fn from_path<F, P>(path: P, f: F) -> Result<ByteViewHandle<'static, T>>
where
F: FnOnce(&'static [u8]) -> Result<T>,
P: AsRef<Path>,
{
ByteViewHandle::from_byteview(ByteView::from_path(path)?, f)
}
pub fn get_bytes<'b>(this: &'b ByteViewHandle<'bytes, T>) -> &'b [u8] {
this.inner.0
}
}
impl<'bytes, T> Deref for ByteViewHandle<'bytes, T> {
type Target = T;
fn deref(&self) -> &T {
&self.inner.1
}
}
impl<'bytes, T> AsRef<T> for ByteViewHandle<'bytes, T> {
fn as_ref(&self) -> &T {
&self.inner.1
}
}