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
use std::borrow::Cow;
use std::fs::File;
use std::io;
use std::ops::Deref;
use std::path::Path;
use std::sync::Arc;
use memmap::Mmap;
use crate::cell::StableDeref;
#[derive(Debug)]
enum ByteViewBacking<'a> {
Buf(Cow<'a, [u8]>),
Mmap(Mmap),
}
impl Deref for ByteViewBacking<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
match *self {
ByteViewBacking::Buf(ref buf) => buf,
ByteViewBacking::Mmap(ref mmap) => mmap,
}
}
}
#[derive(Clone, Debug)]
pub struct ByteView<'a> {
backing: Arc<ByteViewBacking<'a>>,
}
impl<'a> ByteView<'a> {
fn with_backing(backing: ByteViewBacking<'a>) -> Self {
ByteView {
backing: Arc::new(backing),
}
}
pub fn from_cow(cow: Cow<'a, [u8]>) -> Self {
ByteView::with_backing(ByteViewBacking::Buf(cow))
}
pub fn from_slice(buffer: &'a [u8]) -> Self {
ByteView::from_cow(Cow::Borrowed(buffer))
}
pub fn from_vec(buffer: Vec<u8>) -> Self {
ByteView::from_cow(Cow::Owned(buffer))
}
pub fn map_file(file: File) -> Result<Self, io::Error> {
let backing = match unsafe { Mmap::map(&file) } {
Ok(mmap) => ByteViewBacking::Mmap(mmap),
Err(err) => {
if err.kind() == io::ErrorKind::InvalidInput
|| (cfg!(windows) && err.raw_os_error() == Some(1006))
{
ByteViewBacking::Buf(Cow::Borrowed(b""))
} else {
return Err(err);
}
}
};
Ok(ByteView::with_backing(backing))
}
pub fn read<R: io::Read>(mut reader: R) -> Result<Self, io::Error> {
let mut buffer = vec![];
reader.read_to_end(&mut buffer)?;
Ok(ByteView::from_vec(buffer))
}
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
let file = File::open(path)?;
Self::map_file(file)
}
#[inline(always)]
pub fn as_slice(&self) -> &[u8] {
self.backing.deref()
}
}
impl AsRef<[u8]> for ByteView<'_> {
#[inline(always)]
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl Deref for ByteView<'_> {
type Target = [u8];
#[inline(always)]
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
unsafe impl StableDeref for ByteView<'_> {}
#[cfg(test)]
use {failure::Error, std::fs, std::io::Write};
#[test]
fn test_open_empty_file() -> Result<(), Error> {
let mut path = std::env::temp_dir();
path.push(".c0b41a59-801b-4d18-aaa1-88432736116d.empty");
File::create(&path)?;
let bv = ByteView::open(&path)?;
assert_eq!(&*bv, b"");
fs::remove_file(&path)?;
Ok(())
}
#[test]
fn test_open_file() -> Result<(), Error> {
let mut path = std::env::temp_dir();
path.push(".c0b41a59-801b-4d18-aaa1-88432736116d");
let mut file = File::create(&path)?;
file.write_all(b"1234")?;
drop(file);
let bv = ByteView::open(&path)?;
assert_eq!(&*bv, b"1234");
fs::remove_file(&path)?;
Ok(())
}