cargo-binstall/crates/binstalk-manifests/src/helpers.rs
Jiahao XU d4da4680f6
Minor Optimization and fix for bintalk-manifests (#643)
* Optimize `CratesToml::load_from_reader`: Avoid dup monomorphization
* Optimize `CratesToml::write_to_writer`: Avoid dup monomophization
* Optimize `create_if_not_exist`: Take `&path` to avoid dup monomorphization
* Fix `CratesToml::write_to_path`: Use exclusive file lock
   to prevent race conditions.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-01-04 00:37:01 +00:00

13 lines
347 B
Rust

use std::{fs, io, path::Path};
/// Returned file is readable and writable.
pub(crate) fn create_if_not_exist(path: &Path) -> io::Result<fs::File> {
let mut options = fs::File::options();
options.read(true).write(true);
options
.clone()
.create_new(true)
.open(path)
.or_else(|_| options.open(path))
}