pub struct ExternalVector<'a, T>where
    T: Struct,{ /* private fields */ }
Expand description

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

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);

Implementations§

source§

impl<'a, T> ExternalVector<'a, T>where T: Struct,

source

pub fn new(resource_handle: ResourceHandle<'a>) -> Self

Creates an empty ExternalVector<T> in the given resource storage.

source

pub fn len(&self) -> usize

Number of elements that where added to this vector.

source

pub fn is_empty(&self) -> bool

Returns true if no element were added to this vector yet.

source

pub fn grow(&mut self) -> Result<&mut T>

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.

source

pub fn close(self) -> Result<&'a [T], ResourceStorageError>

Flushes the remaining not yet flushed elements in this vector and finalizes the data inside the storage.

An external vector must be closed

Trait Implementations§

source§

impl<T> Debug for ExternalVector<'_, T>where T: Struct,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for ExternalVector<'a, T>

§

impl<'a, T> !Send for ExternalVector<'a, T>

§

impl<'a, T> !Sync for ExternalVector<'a, T>

§

impl<'a, T> Unpin for ExternalVector<'a, T>where T: Unpin,

§

impl<'a, T> !UnwindSafe for ExternalVector<'a, T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.