pub struct File<T> { /* private fields */ }Expand description
An implementation of Persist based on an atomically-updated file at a given path.
An exclusive lock is taken using flock(2) to ensure that concurrent updates cannot
happen, and writes are saved to a staging file before being moved over the old file,
an operation that is atomic on all Unixes.
Implementations§
Source§impl<T: Serialize + DeserializeOwned> File<T>
impl<T: Serialize + DeserializeOwned> File<T>
Sourcepub fn new(path: &Path, value: T) -> Result<Self, Error>
pub fn new(path: &Path, value: T) -> Result<Self, Error>
Creates a new persistent file at path containing value.
Sourcepub fn read(path: &Path) -> Result<Self, Error>
pub fn read(path: &Path) -> Result<Self, Error>
Reads the value from a file at path, returning an error if it does not exist.
Sourcepub fn read_or_create(
path: &Path,
value: impl FnOnce() -> Result<T, Error>,
) -> Result<Self, Error>
pub fn read_or_create( path: &Path, value: impl FnOnce() -> Result<T, Error>, ) -> Result<Self, Error>
Reads the value from a file at path, calling the value function to create it
if it does not exist. If it does exist, value will not be called.
pub fn save(&self) -> Result<(), Error>
Trait Implementations§
Source§impl<T: Serialize + DeserializeOwned + Send> Persist for File<T>
impl<T: Serialize + DeserializeOwned + Send> Persist for File<T>
Source§fn persist(&mut self) -> impl Future<Output = Result<(), Error>>
fn persist(&mut self) -> impl Future<Output = Result<(), Error>>
Writes the value to disk.
The contents of the file need to be over-written completely, so a temporary file is created as a backup in case a crash occurs while writing to disk.
The temporary file is then renamed to the original filename. If serialization or writing to disk fails, the temporary file is deleted.
Source§fn into_value(self) -> T
fn into_value(self) -> T
Takes the value out, releasing the lock on the persistent file.