mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
Impl new fn CratesToml::append_to_file
(#629)
Prepare for fix for #588 * Impl new fn `CratesToml::append_to_file` * Optimize `CratesToml::append_to_file`: Avoid dup monomorphization Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
c87d42d2d8
commit
31c6524833
1 changed files with 32 additions and 22 deletions
|
@ -99,19 +99,15 @@ impl CratesToml<'_> {
|
||||||
self.write_to_file(&mut file)
|
self.write_to_file(&mut file)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append_to_path<'a, Iter>(
|
pub fn append_to_file<'a, Iter>(file: &mut File, iter: Iter) -> Result<(), CratesTomlParseError>
|
||||||
path: impl AsRef<Path>,
|
|
||||||
iter: Iter,
|
|
||||||
) -> Result<(), CratesTomlParseError>
|
|
||||||
where
|
where
|
||||||
Iter: IntoIterator<Item = &'a CrateInfo>,
|
Iter: IntoIterator<Item = &'a CrateInfo>,
|
||||||
{
|
{
|
||||||
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
|
fn inner(
|
||||||
let mut c1 = if file.metadata()?.len() != 0 {
|
file: &mut File,
|
||||||
Self::load_from_reader(&mut *file)?
|
iter: &mut dyn Iterator<Item = &CrateInfo>,
|
||||||
} else {
|
) -> Result<(), CratesTomlParseError> {
|
||||||
Self::default()
|
let mut c1 = CratesToml::load_from_reader(&mut *file)?;
|
||||||
};
|
|
||||||
|
|
||||||
for metadata in iter {
|
for metadata in iter {
|
||||||
let name = &metadata.name;
|
let name = &metadata.name;
|
||||||
|
@ -126,11 +122,25 @@ impl CratesToml<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
file.rewind()?;
|
file.rewind()?;
|
||||||
c1.write_to_file(&mut file)?;
|
c1.write_to_file(file)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inner(file, &mut iter.into_iter())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn append_to_path<'a, Iter>(
|
||||||
|
path: impl AsRef<Path>,
|
||||||
|
iter: Iter,
|
||||||
|
) -> Result<(), CratesTomlParseError>
|
||||||
|
where
|
||||||
|
Iter: IntoIterator<Item = &'a CrateInfo>,
|
||||||
|
{
|
||||||
|
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
|
||||||
|
Self::append_to_file(&mut file, iter)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError>
|
pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError>
|
||||||
where
|
where
|
||||||
Iter: IntoIterator<Item = &'a CrateInfo>,
|
Iter: IntoIterator<Item = &'a CrateInfo>,
|
||||||
|
|
Loading…
Add table
Reference in a new issue