mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 22:30:03 +00:00
Optimize binstalk-{manifest, types} (#610)
* Add new dep maybe-owned v0.3.4 to binstalk-types with features serde * Re-export maybe_owned in binstalk-types * Optimize `CrateSource::url`: Use `MaybeOwned<'static, Url>` to avoid cloning in `CrateSource::cratesio_registry` * Optimize `Source`: Use `MaybeOwned<'static, Url>` to avoid cloning in `Source::cratesio_registry` and `impl From<&CrateSource> for Source`. * Optimize `Source`: Add lifetime `<'a>` to avoid cloning in `impl<'a> From<&'a CrateSource> for Source<'a>` * Optimize `CratesToml::append_to_path`: Avoid cloning caused by `CrateVersionSource::from` by manually `format!` name, version and source into string. * Add new dep beef v0.5.2 to binstalk-manifests with features impl_serde * Optimize `CratesToml::append_to_path`: Eliminate `metadata.bins.clone()` by using `Cow` * Replace dep toml_edit with toml v0.5.10 in binstalk-manifests to speed up compilation and reduce bloat. Previously we switch to toml_edit because it is unmaintained, but now with it moved into toml-rs/toml as a workspace, it is now under active maintenance again, thus we switch back to it. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
e519409ad8
commit
fef8bb6774
7 changed files with 76 additions and 67 deletions
58
Cargo.lock
generated
58
Cargo.lock
generated
|
@ -151,6 +151,15 @@ version = "0.13.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "beef"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a8241f3ebb85c056b509d4327ad0358fbbba6ffb340bf388f26350aeda225b1"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "binstalk"
|
name = "binstalk"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
|
@ -216,6 +225,7 @@ dependencies = [
|
||||||
name = "binstalk-manifests"
|
name = "binstalk-manifests"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"beef",
|
||||||
"binstalk-types",
|
"binstalk-types",
|
||||||
"compact_str",
|
"compact_str",
|
||||||
"detect-targets",
|
"detect-targets",
|
||||||
|
@ -228,7 +238,7 @@ dependencies = [
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"toml_edit",
|
"toml",
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -237,6 +247,7 @@ name = "binstalk-types"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"compact_str",
|
"compact_str",
|
||||||
|
"maybe-owned",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"semver",
|
"semver",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -463,16 +474,6 @@ dependencies = [
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "combine"
|
|
||||||
version = "4.6.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4"
|
|
||||||
dependencies = [
|
|
||||||
"bytes",
|
|
||||||
"memchr",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "compact_str"
|
name = "compact_str"
|
||||||
version = "0.6.1"
|
version = "0.6.1"
|
||||||
|
@ -1341,6 +1342,15 @@ version = "0.1.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
|
checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "maybe-owned"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
|
@ -2436,35 +2446,13 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.5.8"
|
version = "0.5.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
|
checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "toml_datetime"
|
|
||||||
version = "0.5.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "808b51e57d0ef8f71115d8f3a01e7d3750d01c79cac4b3eda910f4389fdf92fd"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "toml_edit"
|
|
||||||
version = "0.15.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b1541ba70885967e662f69d31ab3aeca7b1aaecfcd58679590b893e9239c3646"
|
|
||||||
dependencies = [
|
|
||||||
"combine",
|
|
||||||
"indexmap",
|
|
||||||
"itertools",
|
|
||||||
"serde",
|
|
||||||
"toml_datetime",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower"
|
name = "tower"
|
||||||
version = "0.4.13"
|
version = "0.4.13"
|
||||||
|
|
|
@ -10,6 +10,7 @@ edition = "2021"
|
||||||
license = "Apache-2.0 OR MIT"
|
license = "Apache-2.0 OR MIT"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
beef = { version = "0.5.2", features = ["impl_serde"] }
|
||||||
binstalk-types = { version = "0.1.0", path = "../binstalk-types" }
|
binstalk-types = { version = "0.1.0", path = "../binstalk-types" }
|
||||||
compact_str = { version = "0.6.1", features = ["serde"] }
|
compact_str = { version = "0.6.1", features = ["serde"] }
|
||||||
fs-lock = { version = "0.1.0", path = "../fs-lock" }
|
fs-lock = { version = "0.1.0", path = "../fs-lock" }
|
||||||
|
@ -20,7 +21,7 @@ serde = { version = "1.0.151", features = ["derive"] }
|
||||||
serde-tuple-vec-map = "1.0.1"
|
serde-tuple-vec-map = "1.0.1"
|
||||||
serde_json = "1.0.91"
|
serde_json = "1.0.91"
|
||||||
thiserror = "1.0.38"
|
thiserror = "1.0.38"
|
||||||
toml_edit = { version = "0.15.0", features = ["easy"] }
|
toml = "0.5.10"
|
||||||
url = { version = "2.3.1", features = ["serde"] }
|
url = { version = "2.3.1", features = ["serde"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -15,6 +15,7 @@ use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use beef::Cow;
|
||||||
use compact_str::CompactString;
|
use compact_str::CompactString;
|
||||||
use fs_lock::FileLock;
|
use fs_lock::FileLock;
|
||||||
use home::cargo_home;
|
use home::cargo_home;
|
||||||
|
@ -31,12 +32,12 @@ mod crate_version_source;
|
||||||
use crate_version_source::*;
|
use crate_version_source::*;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct CratesToml {
|
pub struct CratesToml<'a> {
|
||||||
#[serde(with = "tuple_vec_map")]
|
#[serde(with = "tuple_vec_map")]
|
||||||
v1: Vec<(String, Vec<CompactString>)>,
|
v1: Vec<(String, Cow<'a, [CompactString]>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CratesToml {
|
impl CratesToml<'_> {
|
||||||
pub fn default_path() -> Result<PathBuf, CratesTomlParseError> {
|
pub fn default_path() -> Result<PathBuf, CratesTomlParseError> {
|
||||||
Ok(cargo_home()?.join(".crates.toml"))
|
Ok(cargo_home()?.join(".crates.toml"))
|
||||||
}
|
}
|
||||||
|
@ -48,7 +49,7 @@ impl CratesToml {
|
||||||
pub fn load_from_reader<R: io::Read>(mut reader: R) -> Result<Self, CratesTomlParseError> {
|
pub fn load_from_reader<R: io::Read>(mut reader: R) -> Result<Self, CratesTomlParseError> {
|
||||||
let mut vec = Vec::new();
|
let mut vec = Vec::new();
|
||||||
reader.read_to_end(&mut vec)?;
|
reader.read_to_end(&mut vec)?;
|
||||||
Ok(toml_edit::easy::from_slice(&vec)?)
|
Ok(toml::from_slice(&vec)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, CratesTomlParseError> {
|
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, CratesTomlParseError> {
|
||||||
|
@ -59,7 +60,7 @@ impl CratesToml {
|
||||||
/// Only use it when you know that the crate is not in the manifest.
|
/// Only use it when you know that the crate is not in the manifest.
|
||||||
/// Otherwise, you need to call [`CratesToml::remove`] first.
|
/// Otherwise, you need to call [`CratesToml::remove`] first.
|
||||||
pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec<CompactString>) {
|
pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec<CompactString>) {
|
||||||
self.v1.push((cvs.to_string(), bins));
|
self.v1.push((cvs.to_string(), Cow::owned(bins)));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(&mut self, name: &str) {
|
pub fn remove(&mut self, name: &str) {
|
||||||
|
@ -75,7 +76,7 @@ impl CratesToml {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to_writer<W: io::Write>(&self, mut writer: W) -> Result<(), CratesTomlParseError> {
|
pub fn write_to_writer<W: io::Write>(&self, mut writer: W) -> Result<(), CratesTomlParseError> {
|
||||||
let data = toml_edit::easy::to_vec(&self)?;
|
let data = toml::to_vec(&self)?;
|
||||||
writer.write_all(&data)?;
|
writer.write_all(&data)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -108,8 +109,15 @@ impl CratesToml {
|
||||||
};
|
};
|
||||||
|
|
||||||
for metadata in iter {
|
for metadata in iter {
|
||||||
c1.remove(&metadata.name);
|
let name = &metadata.name;
|
||||||
c1.insert(&CrateVersionSource::from(metadata), metadata.bins.clone());
|
let version = &metadata.current_version;
|
||||||
|
let source = Source::from(&metadata.source);
|
||||||
|
|
||||||
|
c1.remove(name);
|
||||||
|
c1.v1.push((
|
||||||
|
format!("{name} {version} ({source})"),
|
||||||
|
Cow::borrowed(&metadata.bins),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
file.rewind()?;
|
file.rewind()?;
|
||||||
|
@ -151,10 +159,10 @@ pub enum CratesTomlParseError {
|
||||||
Io(#[from] io::Error),
|
Io(#[from] io::Error),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TomlParse(#[from] toml_edit::easy::de::Error),
|
TomlParse(#[from] toml::de::Error),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TomlWrite(Box<toml_edit::easy::ser::Error>),
|
TomlWrite(Box<toml::ser::Error>),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
CvsParse(Box<CvsParseError>),
|
CvsParse(Box<CvsParseError>),
|
||||||
|
@ -166,8 +174,8 @@ impl From<CvsParseError> for CratesTomlParseError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<toml_edit::easy::ser::Error> for CratesTomlParseError {
|
impl From<toml::ser::Error> for CratesTomlParseError {
|
||||||
fn from(e: toml_edit::easy::ser::Error) -> Self {
|
fn from(e: toml::ser::Error) -> Self {
|
||||||
CratesTomlParseError::TomlWrite(Box::new(e))
|
CratesTomlParseError::TomlWrite(Box::new(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{borrow::Cow, fmt, str::FromStr};
|
use std::{borrow::Cow, fmt, str::FromStr};
|
||||||
|
|
||||||
use binstalk_types::crate_info::cratesio_url;
|
use binstalk_types::{crate_info::cratesio_url, maybe_owned::MaybeOwned};
|
||||||
use compact_str::CompactString;
|
use compact_str::CompactString;
|
||||||
use miette::Diagnostic;
|
use miette::Diagnostic;
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
|
@ -14,37 +14,45 @@ use crate::crate_info::{CrateInfo, CrateSource, SourceType};
|
||||||
pub struct CrateVersionSource {
|
pub struct CrateVersionSource {
|
||||||
pub name: CompactString,
|
pub name: CompactString,
|
||||||
pub version: Version,
|
pub version: Version,
|
||||||
pub source: Source,
|
pub source: Source<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&CrateInfo> for CrateVersionSource {
|
impl From<&CrateInfo> for CrateVersionSource {
|
||||||
fn from(metadata: &CrateInfo) -> Self {
|
fn from(metadata: &CrateInfo) -> Self {
|
||||||
|
use SourceType::*;
|
||||||
|
|
||||||
|
let url = metadata.source.url.clone();
|
||||||
|
|
||||||
super::CrateVersionSource {
|
super::CrateVersionSource {
|
||||||
name: metadata.name.clone(),
|
name: metadata.name.clone(),
|
||||||
version: metadata.current_version.clone(),
|
version: metadata.current_version.clone(),
|
||||||
source: Source::from(&metadata.source),
|
source: match metadata.source.source_type {
|
||||||
|
Git => Source::Git(url),
|
||||||
|
Path => Source::Path(url),
|
||||||
|
Registry => Source::Registry(url),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
|
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
pub enum Source {
|
pub enum Source<'a> {
|
||||||
Git(Url),
|
Git(MaybeOwned<'a, Url>),
|
||||||
Path(Url),
|
Path(MaybeOwned<'a, Url>),
|
||||||
Registry(Url),
|
Registry(MaybeOwned<'a, Url>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Source {
|
impl Source<'static> {
|
||||||
pub fn cratesio_registry() -> Source {
|
pub fn cratesio_registry() -> Self {
|
||||||
Self::Registry(cratesio_url().clone())
|
Self::Registry(MaybeOwned::Borrowed(cratesio_url()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&CrateSource> for Source {
|
impl<'a> From<&'a CrateSource> for Source<'a> {
|
||||||
fn from(source: &CrateSource) -> Self {
|
fn from(source: &'a CrateSource) -> Self {
|
||||||
use SourceType::*;
|
use SourceType::*;
|
||||||
|
|
||||||
let url = source.url.clone();
|
let url = MaybeOwned::Borrowed(source.url.as_ref());
|
||||||
|
|
||||||
match source.source_type {
|
match source.source_type {
|
||||||
Git => Self::Git(url),
|
Git => Self::Git(url),
|
||||||
|
@ -65,9 +73,9 @@ impl FromStr for CrateVersionSource {
|
||||||
.splitn(2, '+')
|
.splitn(2, '+')
|
||||||
.collect::<Vec<_>>()[..]
|
.collect::<Vec<_>>()[..]
|
||||||
{
|
{
|
||||||
["git", url] => Source::Git(Url::parse(url)?),
|
["git", url] => Source::Git(Url::parse(url)?.into()),
|
||||||
["path", url] => Source::Path(Url::parse(url)?),
|
["path", url] => Source::Path(Url::parse(url)?.into()),
|
||||||
["registry", url] => Source::Registry(Url::parse(url)?),
|
["registry", url] => Source::Registry(Url::parse(url)?.into()),
|
||||||
[kind, arg] => {
|
[kind, arg] => {
|
||||||
return Err(CvsParseError::UnknownSourceType {
|
return Err(CvsParseError::UnknownSourceType {
|
||||||
kind: kind.to_string().into_boxed_str(),
|
kind: kind.to_string().into_boxed_str(),
|
||||||
|
@ -117,7 +125,7 @@ impl fmt::Display for CrateVersionSource {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Source {
|
impl fmt::Display for Source<'_> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Source::Git(url) => write!(f, "git+{url}"),
|
Source::Git(url) => write!(f, "git+{url}"),
|
||||||
|
|
|
@ -11,6 +11,7 @@ license = "Apache-2.0 OR MIT"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
compact_str = { version = "0.6.1", features = ["serde"] }
|
compact_str = { version = "0.6.1", features = ["serde"] }
|
||||||
|
maybe-owned = { version = "0.3.4", features = ["serde"] }
|
||||||
once_cell = "1.16.0"
|
once_cell = "1.16.0"
|
||||||
semver = { version = "1.0.16", features = ["serde"] }
|
semver = { version = "1.0.16", features = ["serde"] }
|
||||||
serde = { version = "1.0.151", features = ["derive"] }
|
serde = { version = "1.0.151", features = ["derive"] }
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
use std::{borrow, cmp, hash};
|
use std::{borrow, cmp, hash};
|
||||||
|
|
||||||
use compact_str::CompactString;
|
use compact_str::CompactString;
|
||||||
|
use maybe_owned::MaybeOwned;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -69,14 +70,14 @@ pub enum SourceType {
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct CrateSource {
|
pub struct CrateSource {
|
||||||
pub source_type: SourceType,
|
pub source_type: SourceType,
|
||||||
pub url: Url,
|
pub url: MaybeOwned<'static, Url>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CrateSource {
|
impl CrateSource {
|
||||||
pub fn cratesio_registry() -> CrateSource {
|
pub fn cratesio_registry() -> CrateSource {
|
||||||
Self {
|
Self {
|
||||||
source_type: SourceType::Registry,
|
source_type: SourceType::Registry,
|
||||||
url: cratesio_url().clone(),
|
url: MaybeOwned::Borrowed(cratesio_url()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,4 @@
|
||||||
pub mod cargo_toml_binstall;
|
pub mod cargo_toml_binstall;
|
||||||
pub mod crate_info;
|
pub mod crate_info;
|
||||||
|
|
||||||
|
pub use maybe_owned;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue