Optimize CratesToml: Use CompactString for bins

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-26 13:48:04 +10:00
parent 834b8bb9c5
commit 2dc246c392
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
3 changed files with 11 additions and 7 deletions

View file

@ -1,13 +1,14 @@
use std::path::{Path, PathBuf};
use cargo_toml::Product;
use compact_str::CompactString;
use log::debug;
use serde::Serialize;
use crate::{atomic_install, atomic_symlink_file, BinstallError, PkgFmt, PkgMeta, Template};
pub struct BinFile {
pub base_name: String,
pub base_name: CompactString,
pub source: PathBuf,
pub dest: PathBuf,
pub link: PathBuf,
@ -15,7 +16,7 @@ pub struct BinFile {
impl BinFile {
pub fn from_product(data: &Data, product: &Product) -> Result<Self, BinstallError> {
let base_name = product.name.clone().unwrap();
let base_name = CompactString::from(product.name.clone().unwrap());
let binary_ext = if data.target.contains("windows") {
".exe"

View file

@ -1,5 +1,7 @@
use std::path::PathBuf;
use compact_str::CompactString;
use crate::{metafiles, DesiredTargets, PkgOverride};
mod resolve;
@ -19,7 +21,7 @@ pub struct Options {
/// MetaData required to update MetaFiles.
pub struct MetaData {
pub bins: Vec<String>,
pub bins: Vec<CompactString>,
pub cvs: metafiles::CrateVersionSource,
pub version_req: String,
pub target: String,

View file

@ -6,6 +6,7 @@ use std::{
path::{Path, PathBuf},
};
use compact_str::CompactString;
use miette::Diagnostic;
use serde::{Deserialize, Serialize};
use thiserror::Error;
@ -15,7 +16,7 @@ use crate::{cargo_home, create_if_not_exist, FileLock};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CratesToml {
v1: BTreeMap<String, Vec<String>>,
v1: BTreeMap<String, Vec<CompactString>>,
}
impl CratesToml {
@ -38,7 +39,7 @@ impl CratesToml {
Self::load_from_reader(file)
}
pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec<String>) {
pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec<CompactString>) {
self.v1.insert(cvs.to_string(), bins);
}
@ -70,7 +71,7 @@ impl CratesToml {
iter: Iter,
) -> Result<(), CratesTomlParseError>
where
Iter: IntoIterator<Item = (&'a CrateVersionSource, Vec<String>)>,
Iter: IntoIterator<Item = (&'a CrateVersionSource, Vec<CompactString>)>,
{
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
let mut c1 = Self::load_from_reader(&mut *file)?;
@ -87,7 +88,7 @@ impl CratesToml {
pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError>
where
Iter: IntoIterator<Item = (&'a CrateVersionSource, Vec<String>)>,
Iter: IntoIterator<Item = (&'a CrateVersionSource, Vec<CompactString>)>,
{
Self::append_to_path(Self::default_path()?, iter)
}