Struct mmap_storage::serializer::FileView[][src]

pub struct FileView<Data, Ser> { /* fields omitted */ }

View on memory mapped File's Data.

In comparsion with File it doesn't hold Data in memory and instead load on demand.

Type parameters:

  • Data - is data's type that can be serialized and de-serialized using serde
  • Ser - describes serde implementation to use

Usage:

extern crate mmap_storage;

use std::fs;
use std::collections::HashMap;

const STORAGE_PATH: &'static str = "some_view.toml";

use mmap_storage::serializer::{
    FileView,
    Toml
};

fn handle_storage() {
    let mut storage = FileView::<HashMap<String, String>, Toml>::open(STORAGE_PATH).expect("To create storage");
    let mut data = match storage.load() {
        Ok(data) => data,
        Err(error) => HashMap::new()
    };
    data.insert(1.to_string(), "".to_string());
    storage.save(&data);
}
fn main() {
    handle_storage();
    let _ = fs::remove_file(STORAGE_PATH);
}

Methods

impl<Data, Ser> FileView<Data, Ser>
[src]

Opens view on file storage.

Opens view on file storage if it exists. If not initialize with default impl.

Note it is error to try to write empty data.

Opens view on file storage if it exists. If not initialize use provided default data.

Note it is error to try to write empty data.

Loads data, if it is available.

If currently file cannot be serialized it returns Error

Loads data, if it is available.

If currently file cannot be serialized it returns Error

Loads data, if it is available or use default impl.

Modifies data via temporary serialization.

This method is useful when working with non-owned data as it is not possible to load and save in the same scope.

NOTE: In case of non-owned data it is valid only until the lifetime of callback execution.

Saves data to file and flushes.

It completely overwrites existing one.

Saves data to file and asynchronously flushes.

It completely overwrites existing one.

Auto Trait Implementations

impl<Data, Ser> Send for FileView<Data, Ser> where
    Data: Send,
    Ser: Send

impl<Data, Ser> Sync for FileView<Data, Ser> where
    Data: Sync,
    Ser: Sync