Struct btlib::atomic_file::AtomicFile
source · pub struct AtomicFile<T> { /* private fields */ }
Expand description
This struct provides access to a value of type T
which can be persisted to a file in the
local filesystem. Access to T
is synchronized within the current process, and access to the
file is synchronized between processes.
This struct is primarily intended to prevent the file storing T
from becoming corrupted if
multiple processes attempt to write to it at once.
Implementations§
source§impl<T: TryDefault + Serialize + DeserializeOwned> AtomicFile<T>
impl<T: TryDefault + Serialize + DeserializeOwned> AtomicFile<T>
sourcepub fn new(path: PathBuf, mask: u32) -> Result<Self>
pub fn new(path: PathBuf, mask: u32) -> Result<Self>
Creates a new AtomicFile stored at path
.
If there is already a file at path
then it is
opened and a value of type T
is deserialized from it. If it does not exist, then a new
value of T
is created using the TryDefault trait which is then written to a new file at
path
.
If a new file is created then it’s mode will be masked using mask
. This means that if a
bit is set in mask
then the corresponding mode bit will be unset in the file.
This method will recursively create all parent directories of path
if they do not exist.
sourcepub fn read(&self) -> Result<AtomicFileReadGuard<'_, T>>
pub fn read(&self) -> Result<AtomicFileReadGuard<'_, T>>
Locks the value of T
for reading in the current process and returns a struct which
dereferences to it.
sourcepub fn write(&self) -> Result<AtomicFileWriteGuard<'_, T>>
pub fn write(&self) -> Result<AtomicFileWriteGuard<'_, T>>
Locks the value of T
for writing in the current process and returns a struct which
dereferences to it.
This struct can be used to save the current value of T
to the file.