Revert back to using Vec<Box<str>> for targets

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-07 12:20:46 +10:00
parent 9ee2609b25
commit ecf6fdbab5
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,59 +1,10 @@
use arrayvec::ArrayVec;
use std::io::{BufRead, Cursor}; use std::io::{BufRead, Cursor};
use std::iter::IntoIterator;
use std::ops::Deref;
use std::process::Output; use std::process::Output;
use std::slice;
use tokio::process::Command; use tokio::process::Command;
/// Compiled target triple, used as default for binary fetching /// Compiled target triple, used as default for binary fetching
pub const TARGET: &str = env!("TARGET"); pub const TARGET: &str = env!("TARGET");
#[derive(Debug, Clone)]
pub struct Targets(ArrayVec<Box<str>, 2>);
impl Targets {
fn from_array<const LEN: usize>(arr: [Box<str>; LEN]) -> Self {
let mut v = ArrayVec::new();
for elem in arr {
v.push(elem);
}
Self(v)
}
fn push(&mut self, s: Box<str>) {
self.0.push(s)
}
}
impl Deref for Targets {
type Target = [Box<str>];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl IntoIterator for Targets {
type Item = Box<str>;
type IntoIter = arrayvec::IntoIter<Box<str>, 2>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a Targets {
type Item = &'a Box<str>;
type IntoIter = slice::Iter<'a, Box<str>>;
fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter()
}
}
/// Detect the targets supported at runtime, /// Detect the targets supported at runtime,
/// which might be different from `TARGET` which is detected /// which might be different from `TARGET` which is detected
/// at compile-time. /// at compile-time.
@ -67,9 +18,9 @@ impl<'a> IntoIterator for &'a Targets {
/// ///
/// Check [this issue](https://github.com/ryankurte/cargo-binstall/issues/155) /// Check [this issue](https://github.com/ryankurte/cargo-binstall/issues/155)
/// for more information. /// for more information.
pub async fn detect_targets() -> Targets { pub async fn detect_targets() -> Vec<Box<str>> {
if let Some(target) = get_target_from_rustc().await { if let Some(target) = get_target_from_rustc().await {
let mut v = Targets::from_array([target]); let mut v = vec![target];
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
if v[0].contains("gnu") { if v[0].contains("gnu") {
@ -93,7 +44,7 @@ pub async fn detect_targets() -> Targets {
} }
#[cfg(not(any(target_os = "linux", target_os = "macos")))] #[cfg(not(any(target_os = "linux", target_os = "macos")))]
{ {
Targets::from_array([TARGET.into()]) vec![TARGET.into()]
} }
} }
} }
@ -117,9 +68,9 @@ async fn get_target_from_rustc() -> Option<Box<str>> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
mod linux { mod linux {
use super::{Command, Output, Targets, TARGET}; use super::{Command, Output, TARGET};
pub(super) async fn detect_targets_linux() -> Targets { pub(super) async fn detect_targets_linux() -> Vec<Box<str>> {
let abi = parse_abi(); let abi = parse_abi();
if let Ok(Output { if let Ok(Output {
@ -134,19 +85,19 @@ mod linux {
} else if let Some(libc_version) = parse_libc_version_from_ldd_output(&stderr) { } else if let Some(libc_version) = parse_libc_version_from_ldd_output(&stderr) {
libc_version libc_version
} else { } else {
return Targets::from_array([create_target_str("musl", abi)]); return vec![create_target_str("musl", abi)];
}; };
if libc_version == "gnu" { if libc_version == "gnu" {
return Targets::from_array([ return vec![
create_target_str("gnu", abi), create_target_str("gnu", abi),
create_target_str("musl", abi), create_target_str("musl", abi),
]); ];
} }
} }
// Fallback to using musl // Fallback to using musl
Targets::from_array([create_target_str("musl", abi)]) vec![create_target_str("musl", abi)]
} }
fn parse_libc_version_from_ldd_output(output: &[u8]) -> Option<&'static str> { fn parse_libc_version_from_ldd_output(output: &[u8]) -> Option<&'static str> {
@ -187,17 +138,16 @@ mod linux {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
mod macos { mod macos {
use super::Targets;
use guess_host_triple::guess_host_triple; use guess_host_triple::guess_host_triple;
pub(super) const AARCH64: &str = "aarch64-apple-darwin"; pub(super) const AARCH64: &str = "aarch64-apple-darwin";
pub(super) const X86: &str = "x86_64-apple-darwin"; pub(super) const X86: &str = "x86_64-apple-darwin";
pub(super) fn detect_targets_macos() -> Targets { pub(super) fn detect_targets_macos() -> Vec<Box<str>> {
if guess_host_triple() == Some(AARCH64) { if guess_host_triple() == Some(AARCH64) {
Targets::from_array([AARCH64.into(), X86.into()]) vec![AARCH64.into(), X86.into()]
} else { } else {
Targets::from_array([X86.into()]) vec![X86.into()]
} }
} }
} }