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

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]

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>[src]

Opens view on file storage.

pub fn open_or_default<'de, P: AsRef<Path>>(path: P) -> Result<Self> where
    Data: Serialize + Deserialize<'de> + Default,
    Ser: Serialization<'de, Data>, 
[src]

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

Note it is error to try to write empty data.

pub fn open_or<'de, P: AsRef<Path>>(path: P, default: &Data) -> Result<Self> where
    Data: Serialize + Deserialize<'de>,
    Ser: Serialization<'de, Data>, 
[src]

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.

pub fn load<'de>(&'de self) -> Result<Data> where
    Data: Serialize + Deserialize<'de>,
    Ser: Serialization<'de, Data>, 
[src]

Loads data, if it is available.

If currently file cannot be serialized it returns Error

pub fn load_owned<'de>(&'de self) -> Result<Data> where
    Data: Serialize + DeserializeOwned,
    Ser: Serialization<'de, Data>, 
[src]

Loads data, if it is available.

If currently file cannot be serialized it returns Error

pub fn load_or_default<'de>(&'de self) -> Data where
    Data: Serialize + Deserialize<'de> + Default,
    Ser: Serialization<'de, Data>, 
[src]

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

pub fn save_bytes(&mut self, bytes: &[u8]) -> Result<()>[src]

Saves raw bytes into file map.

It is not possible to write 0 sizes slices and error shall be returned in this case.

pub fn modify<'de, F: FnOnce(Data) -> Data>(&mut self, cb: F) -> Result<()> where
    Data: Serialize + Deserialize<'de>,
    Ser: Serialization<'de, Data>, 
[src]

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.

pub fn flush_sync(&mut self) -> Result<()>[src]

Synchronously flushes changes of file.

pub fn flush_async(&mut self) -> Result<()>[src]

Asynchronously flushes changes of file.

pub fn save<'de>(&mut self, data: &Data) -> Result<()> where
    Data: Serialize + Deserialize<'de>,
    Ser: Serialization<'de, Data>, 
[src]

Saves data to file.

It completely overwrites existing one.

pub fn save_sync<'de>(&mut self, data: &Data) -> Result<()> where
    Data: Serialize + Deserialize<'de>,
    Ser: Serialization<'de, Data>, 
[src]

Saves data to file and flushes synchronously to policy.

pub fn save_async<'de>(&mut self, data: &Data) -> Result<()> where
    Data: Serialize + Deserialize<'de>,
    Ser: Serialization<'de, Data>, 
[src]

Saves data to file and flushes asynchronously to policy.

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]