Struct config_file_handler::FileHandler
[−]
[src]
pub struct FileHandler { // some fields omitted }
Struct for reading and writing config files.
Thread- and Process-Safety
Since all instances of FileHandler
initialised with the same value for name
within a single
process will likely refer to the same file on disk, it is not safe to access any such instance
concurrently with that same or any other such instance (with the exception of the
path()
function which is the only non-mutating member function).
For instances initialised with different values for name
, it is safe to access separate
instances concurrently.
It is possibly unsafe to call write_file()
concurrently with a different
process calling read_file()
or write_file()
where
both processes have the same name and their instances of FileHandler
are using the same name,
since these may be accessing the same file on disk. However, it is safe to call
read_file()
concurrently across multiple such processes, since this
function doesn't modify the file.
Perhaps the easiest way to make multi-process access safe is to ensure each process is a single
execution of a binary, and that each binary is located in a directory which is
mutually-exclusive to all other such binaries, and that each config file to be managed by
FileHandler
is placed in each binary's current_bin_dir()
. In
this way, each process should be the only one accessing that file.
Methods
impl FileHandler
[src]
fn new<S: AsRef<OsStr> + ?Sized>(name: &S) -> Result<FileHandler, Error>
Constructor taking the required file name (not the full path)
This function tests whether it has write access to the file in the following locations in this order (see also an example config file flowchart):
See Thread- and Process-Safety for notes on thread- and process-safety.
fn cleanup<S: AsRef<OsStr>>(name: &S) -> Result<()>
Remove the file from every location where it can be read.
fn path(&self) -> &Path
Get the full path to the file.
fn read_file<Contents: Decodable>(&self) -> Result<Contents, Error>
Read the contents of the file and decode it as JSON.
fn write_file<Contents: Encodable>(&self, contents: &Contents) -> Result<(), Error>
Write contents
to the file as JSON.