Skip to content

Commit

Permalink
Use a const and binary search instead of a match
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Oct 31, 2024
1 parent f694c4c commit ae310a3
Show file tree
Hide file tree
Showing 3 changed files with 3,080 additions and 2,248 deletions.
29 changes: 13 additions & 16 deletions dev-tools/gen-target-info/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ const PRELUDE: &str = r#"//! This file is generated code. Please edit the genera

fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std::io::Result<()> {
writeln!(f, "use super::Target;")?;
writeln!(f, "use std::borrow::Cow;")?;
writeln!(f)?;
writeln!(
f,
"pub(crate) fn get(target_triple: &str) -> Option<Target> {{"
)?;
writeln!(f, " Some(match target_triple {{")?;
writeln!(f, "pub(crate) const LIST: &[(&str, Target)] = &[")?;

for (triple, spec) in &target_specs.0 {
let full_arch = triple.split_once('-').unwrap().0;
Expand All @@ -27,20 +24,20 @@ fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std
let env = spec.env.as_deref().unwrap_or("");
let abi = spec.abi.as_deref().unwrap_or("");

writeln!(f, " {triple:?} => Target {{")?;
writeln!(f, " full_arch: {full_arch:?}.into(),")?;
writeln!(f, " arch: {arch:?}.into(),")?;
writeln!(f, " vendor: {vendor:?}.into(),")?;
writeln!(f, " os: {os:?}.into(),")?;
writeln!(f, " env: {env:?}.into(),")?;
writeln!(f, " abi: {abi:?}.into(),")?;
writeln!(f, " (")?;
writeln!(f, " {triple:?},")?;
writeln!(f, " Target {{")?;
writeln!(f, " full_arch: Cow::Borrowed({full_arch:?}),")?;
writeln!(f, " arch: Cow::Borrowed({arch:?}),")?;
writeln!(f, " vendor: Cow::Borrowed({vendor:?}),")?;
writeln!(f, " os: Cow::Borrowed({os:?}),")?;
writeln!(f, " env: Cow::Borrowed({env:?}),")?;
writeln!(f, " abi: Cow::Borrowed({abi:?}),")?;
writeln!(f, " }},")?;
writeln!(f, " ),")?;
}

writeln!(f, " _ => return None,")?;

writeln!(f, " }})")?;
writeln!(f, "}}")?;
writeln!(f, "];")?;

Ok(())
}
Expand Down
9 changes: 6 additions & 3 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod generated;
/// The parts of `rustc`'s target triple.
///
/// See <https://doc.rust-lang.org/cargo/appendix/glossary.html#target>.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub(crate) struct Target {
/// The full architecture, including the subarchitecture.
///
Expand Down Expand Up @@ -118,8 +118,11 @@ impl FromStr for Target {

/// This will fail when using a custom target triple unknown to `rustc`.
fn from_str(target_triple: &str) -> Result<Self, Error> {
if let Some(target) = generated::get(target_triple) {
Ok(target)
if let Ok(index) =
generated::LIST.binary_search_by_key(&target_triple, |(target_triple, _)| target_triple)
{
let (_, target) = &generated::LIST[index];
Ok(target.clone())
} else {
Err(Error::new(
ErrorKind::InvalidTarget,
Expand Down
Loading

0 comments on commit ae310a3

Please sign in to comment.