[−][src]Struct mmap_storage::serializer::FileView
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 usingserde
Ser
- describesserde
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]
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.
pub fn open_or<'de, P: AsRef<Path>>(path: P, default: &Data) -> Result<Self> where
Data: Serialize + Deserialize<'de>,
Ser: Serialization<'de, Data>,
[src]
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.
pub fn load<'de>(&'de self) -> Result<Data> where
Data: Serialize + Deserialize<'de>,
Ser: Serialization<'de, Data>,
[src]
Data: Serialize + Deserialize<'de>,
Ser: Serialization<'de, Data>,
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]
Data: Serialize + DeserializeOwned,
Ser: Serialization<'de, Data>,
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]
Data: Serialize + Deserialize<'de> + Default,
Ser: Serialization<'de, Data>,
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]
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.
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]
Data: Serialize + Deserialize<'de>,
Ser: Serialization<'de, Data>,
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]
Data: Serialize + Deserialize<'de>,
Ser: Serialization<'de, Data>,
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]
Data: Serialize + Deserialize<'de>,
Ser: Serialization<'de, Data>,
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,
Data: Sync,
Ser: Sync,
impl<Data, Ser> Unpin for FileView<Data, Ser> where
Data: Unpin,
Ser: Unpin,
Data: Unpin,
Ser: Unpin,
impl<Data, Ser> Send for FileView<Data, Ser> where
Data: Send,
Ser: Send,
Data: Send,
Ser: Send,
impl<Data, Ser> RefUnwindSafe for FileView<Data, Ser> where
Data: RefUnwindSafe,
Ser: RefUnwindSafe,
Data: RefUnwindSafe,
Ser: RefUnwindSafe,
impl<Data, Ser> UnwindSafe for FileView<Data, Ser> where
Data: UnwindSafe,
Ser: UnwindSafe,
Data: UnwindSafe,
Ser: UnwindSafe,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,