FileView

Struct FileView 

Source
pub struct FileView<Data, Ser> { /* private fields */ }
Expand description

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

Implementations§

Source§

impl<Data, Ser> FileView<Data, Ser>

Source

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

Opens view on file storage.

Source

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

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

Note it is error to try to write empty data.

Source

pub fn open_or<'de, P: AsRef<Path>>(path: P, default: &Data) -> Result<Self>
where Data: Serialize + Deserialize<'de>, Ser: Serialization<'de, 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.

Source

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

Loads data, if it is available.

If currently file cannot be serialized it returns Error

Source

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

Loads data, if it is available.

If currently file cannot be serialized it returns Error

Source

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

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

Source

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

Saves raw bytes into file map.

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

Source

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

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.

Source

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

Synchronously flushes changes of file.

Source

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

Asynchronously flushes changes of file.

Source

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

Saves data to file.

It completely overwrites existing one.

Source

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

Saves data to file and flushes synchronously to policy.

Source

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

Saves data to file and flushes asynchronously to policy.

Auto Trait Implementations§

§

impl<Data, Ser> Freeze for FileView<Data, Ser>

§

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

§

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,

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.