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 usingserdeSer- describesserdeimplementation 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>
impl<Data, Ser> FileView<Data, Ser>
Sourcepub fn open_or_default<'de, P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open_or_default<'de, P: AsRef<Path>>(path: P) -> Result<Self>
Opens view on file storage if it exists. If not initialize with default impl.
Note it is error to try to write empty data.
Sourcepub fn open_or<'de, P: AsRef<Path>>(path: P, default: &Data) -> Result<Self>
pub fn open_or<'de, P: AsRef<Path>>(path: P, default: &Data) -> Result<Self>
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.
Sourcepub fn load<'de>(&'de self) -> Result<Data>
pub fn load<'de>(&'de self) -> Result<Data>
Loads data, if it is available.
If currently file cannot be serialized it returns Error
Sourcepub fn load_owned<'de>(&'de self) -> Result<Data>
pub fn load_owned<'de>(&'de self) -> Result<Data>
Loads data, if it is available.
If currently file cannot be serialized it returns Error
Sourcepub fn load_or_default<'de>(&'de self) -> Data
pub fn load_or_default<'de>(&'de self) -> Data
Loads data, if it is available or use default impl.
Sourcepub fn save_bytes(&mut self, bytes: &[u8]) -> Result<()>
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.
Sourcepub fn modify<'de, F: FnOnce(Data) -> Data>(&mut self, cb: F) -> Result<()>
pub fn modify<'de, F: FnOnce(Data) -> Data>(&mut self, cb: F) -> Result<()>
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.
Sourcepub fn flush_sync(&mut self) -> Result<()>
pub fn flush_sync(&mut self) -> Result<()>
Synchronously flushes changes of file.
Sourcepub fn flush_async(&mut self) -> Result<()>
pub fn flush_async(&mut self) -> Result<()>
Asynchronously flushes changes of file.
Sourcepub fn save<'de>(&mut self, data: &Data) -> Result<()>
pub fn save<'de>(&mut self, data: &Data) -> Result<()>
Saves data to file.
It completely overwrites existing one.
Sourcepub fn save_sync<'de>(&mut self, data: &Data) -> Result<()>
pub fn save_sync<'de>(&mut self, data: &Data) -> Result<()>
Saves data to file and flushes synchronously to policy.
Sourcepub fn save_async<'de>(&mut self, data: &Data) -> Result<()>
pub fn save_async<'de>(&mut self, data: &Data) -> Result<()>
Saves data to file and flushes asynchronously to policy.