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
extern crate serde;
use self::serde::{Serialize, Deserialize, de::DeserializeOwned};
use ::std::default::Default;
use ::std::marker;
use ::std::io;
use ::std::path;
use ::std::mem;
use ::file as inner;
pub trait Serialization<'de, T: Serialize + Deserialize<'de>>: Default {
fn serialize(data: &T) -> Result<Vec<u8>, io::Error>;
fn deserialize(bytes: &'de [u8]) -> Result<T, io::Error>;
}
#[cfg(feature = "ser_toml")]
pub mod toml;
#[cfg(feature = "ser_toml")]
pub use self::toml::Toml;
#[cfg(feature = "ser_json")]
pub mod json;
#[cfg(feature = "ser_json")]
pub use self::json::Json;
#[inline(always)]
fn empty_write_error() -> io::Error {
io::Error::new(io::ErrorKind::InvalidInput, "Attempt to write data that serializes into empty bytes sequence")
}
pub struct FileView<Data, Ser> {
inner: inner::Storage,
_data: marker::PhantomData<Data>,
_ser: marker::PhantomData<Ser>,
}
impl<Data, Ser> FileView<Data, Ser> {
pub fn open<P: AsRef<path::Path>>(path: P) -> io::Result<Self> {
inner::Storage::open(path).map(|mmap| Self {
inner: mmap,
_data: marker::PhantomData,
_ser: marker::PhantomData,
})
}
pub fn open_or_default<'de, P: AsRef<path::Path>>(path: P) -> io::Result<Self> where Data: Serialize + Deserialize<'de> + Default, Ser: Serialization<'de, Data> {
let path = path.as_ref();
let inner = match inner::Storage::open_exising(path) {
Ok(inner) => inner,
Err(_) => {
let default = Ser::serialize(&Data::default())?;
if default.len() == 0 {
return Err(empty_write_error());
}
let mut inner = inner::Storage::open(path)?;
inner.put_data(&default)?;
inner.flush_sync()?;
inner
}
};
Ok(Self {
inner,
_data: marker::PhantomData,
_ser: marker::PhantomData,
})
}
pub fn open_or<'de, P: AsRef<path::Path>>(path: P, default: &Data) -> io::Result<Self>
where Data: Serialize + Deserialize<'de>, Ser: Serialization<'de, Data> {
let path = path.as_ref();
let inner = match inner::Storage::open_exising(path) {
Ok(inner) => inner,
Err(_) => {
let default = Ser::serialize(&default)?;
if default.len() == 0 {
return Err(empty_write_error());
}
let mut inner = inner::Storage::open(path)?;
inner.put_data(&default)?;
inner.flush_sync()?;
inner
}
};
Ok(Self {
inner,
_data: marker::PhantomData,
_ser: marker::PhantomData,
})
}
pub fn load<'de>(&'de self) -> io::Result<Data> where Data: Serialize + Deserialize<'de>, Ser: Serialization<'de, Data> {
Ser::deserialize(self.inner.as_slice())
}
pub fn load_owned<'de>(&'de self) -> io::Result<Data> where Data: Serialize + DeserializeOwned, Ser: Serialization<'de, Data> {
Ser::deserialize(self.inner.as_slice())
}
pub fn load_or_default<'de>(&'de self) -> Data where Data: Serialize + Deserialize<'de> + Default, Ser: Serialization<'de, Data> {
Ser::deserialize(self.inner.as_slice()).unwrap_or(Data::default())
}
pub fn modify<'de, F: FnOnce(Data) -> Data>(&mut self, cb: F) -> io::Result<()> where Data: Serialize + Deserialize<'de>, Ser: Serialization<'de, Data> {
let new_data = {
let inner: &inner::Storage = unsafe { mem::transmute(&self.inner) };
let mut data = Ser::deserialize(inner.as_slice())?;
let data = cb(data);
Ser::serialize(&data)?
};
if new_data.len() == 0 {
Err(empty_write_error())
} else {
self.inner.put_data(&new_data)?;
self.inner.flush_sync()
}
}
pub fn save<'de>(&mut self, data: &Data) -> io::Result<()> where Data: Serialize + Deserialize<'de>, Ser: Serialization<'de, Data> {
let data = Ser::serialize(&data)?;
if data.len() == 0 {
Err(empty_write_error())
} else {
self.inner.put_data(&data)
}
}
#[inline]
pub fn save_sync<'de>(&mut self, data: &Data) -> io::Result<()> where Data: Serialize + Deserialize<'de>, Ser: Serialization<'de, Data> {
self.save(&data)?;
self.inner.flush_sync()
}
#[inline]
pub fn save_async<'de>(&mut self, data: &Data) -> io::Result<()> where Data: Serialize + Deserialize<'de>, Ser: Serialization<'de, Data> {
self.save(&data)?;
self.inner.flush_async()
}
}