Skip to content

Commit

Permalink
Optimize TargetTriple: Use Cow<'static, str> (#1161)
Browse files Browse the repository at this point in the history
instead of `CompactString` since `target_lexicon::{OperatingSystem,
Architecture, Environment}::into_str()` will return a `&'static str`
most of the time.

Also updated `SUPPORT.md`.

Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu authored Jun 21, 2023
1 parent abecd9a commit 138112c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion SUPPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ With the following configuration keys:


`pkg-url` and `bin-dir` are templated to support different names for different versions / architectures / etc.
Template variables use the format `{ VAR }` where `VAR` is the name of the variable, with the following variables available:
Template variables use the format `{ VAR }` where `VAR` is the name of the variable,
`\{` for literal `{`, `\}` for literal `}` and `\\` for literal `\`,
with the following variables available:
- `name` is the name of the crate/package
- `version` is the crate version (per `--version` and the crate manifest)
- `repo` is the repository linked in `Cargo.toml`
Expand All @@ -34,6 +36,16 @@ Template variables use the format `{ VAR }` where `VAR` is the name of the varia
- `archive-format` is the soft-deprecated filename extension of the package archive format that does not include the prefix `.`, e.g. `tgz` for tgz or `exe`/`""` for bin.
- `binary-ext` is the string `.exe` if the `target` is for Windows, or the empty string otherwise
- `format` is a soft-deprecated alias for `archive-format` in `pkg-url`, and alias for `binary-ext` in `bin-dir`; in the future, this may warn at install time.
- `target-family`: Operating system of the target from [`target_lexicon::OperatingSystem`]
- `target-arch`: Architecture of the target, `universal` on `{universal, universal2}-apple-darwin`,
otherwise from [`target_lexicon::Architecture`]
- `target-libc`: ABI environment of the target from [`target_lexicon::Environment`]
- `target-vendor`: Vendor of the target from [`target_lexicon::Vendor`]

[`target_lexicon::OperatingSystem`]: https://docs.rs/target-lexicon/latest/target_lexicon/enum.OperatingSystem.html
[`target_lexicon::Architecture`]: https://docs.rs/target-lexicon/latest/target_lexicon/enum.Architecture.html
[`target_lexicon::Environment`]: https://docs.rs/target-lexicon/latest/target_lexicon/enum.Environment.html
[`target_lexicon::Vendor`]: https://docs.rs/target-lexicon/latest/target_lexicon/enum.Vendor.html

`pkg-url`, `pkg-fmt` and `bin-dir` can be overridden on a per-target basis if required, for example, if your `x86_64-pc-windows-msvc` builds use `zip` archives this could be set via:

Expand Down
2 changes: 1 addition & 1 deletion crates/binstalk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ once_cell = "1.18.0"
semver = { version = "1.0.17", features = ["serde"] }
serde = { version = "1.0.163", features = ["derive"] }
strum = "0.25.0"
target-lexicon = { version = "0.12.7", features = ["std"] }
target-lexicon = { version = "0.12.8", features = ["std"] }
tempfile = "3.5.0"
thiserror = "1.0.40"
# parking_lot for `tokio::sync::OnceCell::const_new`
Expand Down
16 changes: 7 additions & 9 deletions crates/binstalk/src/helpers/target_triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use crate::{errors::BinstallError, helpers::is_universal_macos};

#[derive(Clone, Debug)]
pub struct TargetTriple {
// TODO: Once https://github.com/bytecodealliance/target-lexicon/pull/90
// lands, consider replacing use of CompactString with `Cow<'_, str>`.
pub target_family: CompactString,
pub target_arch: CompactString,
pub target_libc: CompactString,
pub target_family: Cow<'static, str>,
pub target_arch: Cow<'static, str>,
pub target_libc: Cow<'static, str>,
pub target_vendor: CompactString,
}

Expand All @@ -28,13 +26,13 @@ impl FromStr for TargetTriple {
let triple = Triple::from_str(s)?;

Ok(Self {
target_family: triple.operating_system.to_compact_string(),
target_family: triple.operating_system.into_str(),
target_arch: if is_universal_macos {
"universal".to_compact_string()
Cow::Borrowed("universal")
} else {
triple.architecture.to_compact_string()
triple.architecture.into_str()
},
target_libc: triple.environment.to_compact_string(),
target_libc: triple.environment.into_str(),
target_vendor: triple.vendor.to_compact_string(),
})
}
Expand Down

0 comments on commit 138112c

Please sign in to comment.