flatdata 0.5.3

Rust implementation of flatdata
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::{
    error::ResourceStorageError,
    structs::{NoOverlap, Overlap, Struct},
    SliceExt,
};

use crate::storage::ResourceHandle;

use std::{borrow::BorrowMut, fmt, io, slice::SliceIndex};

/// A container holding a contiguous sequence of flatdata structs of the same
/// type `T` in memory, and providing read and write access to it.
///
/// Vector data is fully stored and populated in memory before it is
/// serialized. This container is often used for data which needs to be changed
/// or updated after insertion in the container. When data can be incrementally
/// serialized without later updates, [`ExternalVector`] is usually a
/// better choice since it may decrease the memory footprint of serialization
/// significantly.
///
/// An archive builder provides a setter for each vector resource. Use
/// [`as_view`] and the corresponding setter to write a `Vector` to storage.
///
/// # Examples
/// ``` flatdata
/// struct A {
///     x : u32 : 16;
///     y : u32 : 16;
/// }
///
/// archive X {
///    data : vector< A >;
/// }
/// ```
///
/// ```
/// use flatdata::{ MemoryResourceStorage,  Vector };
/// use flatdata::test::{A, X, XBuilder};
///
/// let storage = MemoryResourceStorage::new("/root/extvec");
/// let builder = XBuilder::new(storage.clone()).expect("failed to create builder");
/// let mut v: Vector<A> = Vector::new();
/// let mut a = v.grow();
/// a.set_x(1);
/// a.set_y(2);
///
/// let mut b = v.grow();
/// b.set_x(3);
/// b.set_y(4);
///
/// assert_eq!(v.len(), 2);
/// builder.set_data(&v.as_view());
///
/// let archive = X::open(storage).expect("failed to open");
/// let view = archive.data();
/// assert_eq!(view[1].x(), 3);
/// ```
///
/// [`ExternalVector`]: struct.ExternalVector.html
/// [`as_view`]: #method.as_view
pub struct Vector<T>
where
    T: Struct,
{
    data: Vec<T>,
}

impl<T> Vector<T>
where
    T: Struct,
{
    /// Creates an empty `Vector<T>`.
    #[inline]
    pub fn new() -> Self {
        Self::with_len(0)
    }

    /// Resets the 'Vector<T>' to its initial empty state.
    #[inline]
    pub fn clear(&mut self) {
        self.data.clear();
        self.data.push(unsafe { T::create_unchecked() });
    }

    /// Creates a `Vector<T>` with `len` many elements.
    ///
    /// `T`'s fields are all filled with zeroes.
    #[inline]
    pub fn with_len(len: usize) -> Self {
        let mut data = Vec::with_capacity(len + 1);
        data.resize_with(len + 1, || unsafe { T::create_unchecked() });
        Self { data }
    }

    /// Reserves capacity for at least `additional` more elements to be
    /// inserted in the given vector. The collection may reserve more space
    /// to avoid frequent reallocations. After calling reserve, capacity
    /// will be greater than or equal to `self.len() + additional`. Does nothing
    /// if capacity is already sufficient.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.data.reserve(self.data.len() + additional)
    }

    /// Returns a slice for the vector contents.
    ///
    /// Hides sentinels in case T IS_OVERLAPPING_WITH_NEXT,
    /// i.e. a vector of size 4 get's converted into a slice of size 3
    #[inline]
    pub fn as_view(&self) -> &[T] {
        if T::IS_OVERLAPPING_WITH_NEXT {
            &self.data[0..self.data.len().saturating_sub(2)]
        } else {
            &self.data[0..self.data.len() - 1]
        }
    }

    /// Appends an element to the end of this vector and returns a mutable
    /// handle to it.
    #[inline]
    pub fn grow(&mut self) -> &mut T {
        let next = self.len();
        self.data.push(unsafe { T::create_unchecked() });
        &mut self.data[next]
    }
}

impl<T> std::ops::Deref for Vector<T>
where
    T: Struct,
{
    type Target = [T];

    fn deref(&self) -> &[T] {
        let len = self.data.len() - 1;
        &self.data[0..len]
    }
}

/// Safety: We must not implement DerefMut if T is not NoOverlap,
/// since it would allow getting a mutable refernce to one element,
/// and refernece to the next: Both use the same memory address for data of ranges
impl<T> std::ops::DerefMut for Vector<T>
where
    T: Struct + NoOverlap,
{
    fn deref_mut(&mut self) -> &mut [T] {
        let len = self.data.len() - 1;
        &mut self.data[0..len]
    }
}

impl<T, Idx> std::ops::Index<Idx> for Vector<T>
where
    T: Struct + Overlap,
    Idx: SliceIndex<[T]>,
{
    type Output = Idx::Output;
    fn index(&self, index: Idx) -> &Idx::Output {
        &self.data[index]
    }
}

/// Manually implement IndexMut, since DerefMut is not always implemented
/// Safety: We must not implement IndexMut for ranges if T is not NoOverlap
impl<T> std::ops::IndexMut<usize> for Vector<T>
where
    T: Struct + Overlap,
{
    fn index_mut(&mut self, index: usize) -> &mut T {
        &mut self.data[index]
    }
}

impl<T> Default for Vector<T>
where
    T: Struct,
{
    /// Creates an empty `Vector<T>`.
    fn default() -> Self {
        Vector::new()
    }
}

impl<T> fmt::Debug for Vector<T>
where
    T: Struct + std::fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.as_view().fmt(f)
    }
}

/// Vector which flushes its content when growing.
///
/// Useful for serialization of data which does not fully fit in memory.
///
/// External vector does not provide access to elements previously added to it.
/// Only the last element added to the vector using the result of the method
/// [`grow`] can be accessed and written.
///
/// An external vector *must* be closed, after the last element was written to
/// it. After closing, it can not be used anymore.
///
/// # Examples
/// ``` flatdata
/// struct A {
///     x : u32 : 16;
///     y : u32 : 16;
/// }
///
/// archive X {
///    data : vector< A >;
/// }
/// ```
///
/// ```
/// use flatdata::MemoryResourceStorage;
/// use flatdata::test::{A, X, XBuilder};
///
/// let storage = MemoryResourceStorage::new("/root/extvec");
/// let builder = XBuilder::new(storage.clone()).expect("failed to create builder");
/// {
///     let mut v = builder.start_data().expect("failed to start");
///     let mut a = v.grow().expect("grow failed");
///     a.set_x(0);
///     a.set_y(1);
///
///     let mut a = v.grow().expect("grow failed");
///     a.set_x(2);
///     a.set_y(3);
///
///     let view = v.close().expect("close failed");
///
///     // data can also be inspected directly after closing
///     assert_eq!(view.len(), 2);
///     assert_eq!(view[0].x(), 0);
///     assert_eq!(view[0].y(), 1);
/// }
///
/// let archive = X::open(storage).expect("failed to open");
/// let view = archive.data();
/// assert_eq!(view[1].x(), 2);
/// assert_eq!(view[1].y(), 3);
/// ```
///
/// [`grow`]: #method.grow
pub struct ExternalVector<'a, T>
where
    T: Struct,
{
    data: Vector<T>,
    len: usize,
    resource_handle: ResourceHandle<'a>,
}

impl<'a, T> ExternalVector<'a, T>
where
    T: Struct,
{
    /// Creates an empty `ExternalVector<T>` in the given resource storage.
    pub fn new(resource_handle: ResourceHandle<'a>) -> Self {
        Self {
            data: Vector::new(),
            len: 0,
            resource_handle,
        }
    }

    /// Number of elements that where added to this vector.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns `true` if no element were added to this vector yet.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Appends an element to the end of this vector and returns a mutable
    /// handle to it.
    ///
    /// Calling this method may flush data to storage (cf. [`flush`]), which
    /// may fail due to different IO reasons.
    ///
    /// [`flush`]: #method.flush
    pub fn grow(&mut self) -> io::Result<&mut T> {
        if self.data.as_view().as_bytes().len() > 1024 * 1024 * 32 {
            self.flush()?;
        }
        self.len += 1;
        Ok(self.data.grow())
    }

    /// Flushes the not yet flushed content in this vector to storage.
    fn flush(&mut self) -> io::Result<()> {
        self.resource_handle
            .borrow_mut()
            .write(&self.data.as_view().as_bytes())?;
        self.data.clear();
        Ok(())
    }

    /// Flushes the remaining not yet flushed elements in this vector and
    /// finalizes the data inside the storage.
    ///
    /// An external vector *must* be closed
    pub fn close(mut self) -> Result<&'a [T], ResourceStorageError> {
        if self.data.len() > 0 || self.len == 0 {
            self.flush().map_err(|e| {
                ResourceStorageError::from_io_error(e, self.resource_handle.name().into())
            })?;
        }
        self.resource_handle
            .close()
            .map(|data| <&[T]>::from_bytes(data).expect("Corrupted data"))
    }
}

impl<T> fmt::Debug for ExternalVector<'_, T>
where
    T: Struct,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "ExternalVector {{ len: {} }}", self.len())
    }
}

#[cfg(test)]
#[allow(dead_code)]
mod tests {
    // Note: ExternalVector is tested in the corresponding example.

    use super::*;
    use crate::test::{A, R};

    #[test]
    fn test_vector_new() {
        let v: Vector<A> = Vector::new();
        assert_eq!(v.len(), 0);
    }

    #[test]
    fn test_vector_range() {
        let mut v: Vector<R> = Vector::with_len(3);
        v[0].set_first_x(10);
        v[1].set_first_x(20);
        v[2].set_first_x(30);

        assert_eq!(v[0].x(), 10..20);
        assert_eq!(v[1].x(), 20..30);
        assert_eq!(v[2].x(), 30..0);
    }

    #[test]
    fn test_vector_index() {
        let mut v: Vector<A> = Vector::with_len(2);
        assert_eq!(v.len(), 2);
        {
            let a = &mut v[0];
            a.set_x(1);
            a.set_y(2);
            assert_eq!(a.x(), 1);
            assert_eq!(a.y(), 2);
        }
        {
            let b = &mut v[1];
            b.set_x(3);
            b.set_y(4);
            assert_eq!(b.x(), 3);
            assert_eq!(b.y(), 4);
        }
        let a = &mut v[0];
        assert_eq!(a.x(), 1);
        assert_eq!(a.y(), 2);
        let b = &mut v[1];
        assert_eq!(b.x(), 3);
        assert_eq!(b.y(), 4);
    }

    #[test]
    fn test_vector_as_view() {
        let mut v: Vector<A> = Vector::with_len(1);
        assert_eq!(v.len(), 1);
        {
            let a = &mut v[0];
            a.set_x(1);
            assert_eq!(a.x(), 1);
            a.set_y(2);
            assert_eq!(a.y(), 2);
        }
        let view = v.as_view();
        let a = &view[0];
        assert_eq!(a.x(), 1);
        assert_eq!(a.y(), 2);
    }

    #[test]
    fn test_vector_grow() {
        let mut v: Vector<A> = Vector::with_len(1);
        assert_eq!(v.len(), 1);
        {
            let a = &mut v[0];
            a.set_x(1);
            a.set_y(2);
            assert_eq!(a.x(), 1);
            assert_eq!(a.y(), 2);
        }
        {
            let b = v.grow();
            b.set_x(3);
            b.set_y(4);
            assert_eq!(b.x(), 3);
            assert_eq!(b.y(), 4);
        }
        {
            assert_eq!(v.len(), 2);
            let a = &v[0];
            assert_eq!(a.x(), 1);
            assert_eq!(a.y(), 2);
            let b = &v[1];
            assert_eq!(b.x(), 3);
            assert_eq!(b.y(), 4);
        }
        v.grow();
        assert_eq!(v.len(), 3);
    }

    #[test]
    fn test_ability_to_get_mut_and_const_for_non_overlap() {
        let mut v: Vector<A> = Vector::with_len(10);
        let mut_v_ref = &mut v[..];
        let (left, right) = mut_v_ref.split_at_mut(5);
        let x = &mut left[4];
        let y = &mut right[0];
        y.set_x(10);
        assert_eq!(y.x(), 10);
        assert_eq!(x.x(), 0);
    }
}