Skip to content

Commit

Permalink
Use the counted_array macro for the ARGS arrays
Browse files Browse the repository at this point in the history
One of the most annoying thing from using static arrays in rust is that
there is no size inference, and you end up having to give the proper
size. Which makes updates to the arrays cumbersome.

I was reading "This Week in Rust 252", which linked to (RFC: Elide array
size)[rust-lang/rfcs#2545], set to address this
very problem, and @durka linked to his counted-arrays crate, which
already existed and essentially implements the idea behind the RFC.
  • Loading branch information
glandium committed Sep 19, 2018
1 parent dda88a6 commit aeedd7e
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ byteorder = "1.0"
bytes = "0.4"
chrono = { version = "0.4", optional = true }
clap = "2.23.0"
counted-array = "0.1"
directories = "1"
env_logger = "0.5"
error-chain = { version = "0.12", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl CCompilerImpl for Clang {
}
}

pub static ARGS: [ArgInfo<gcc::ArgData>; 10] = [
counted_array!(pub static ARGS: [ArgInfo<gcc::ArgData>; _] = [
take_arg!("--serialize-diagnostics", OsString, Separated, PassThrough),
take_arg!("--target", OsString, Separated, PassThrough),
// TODO: should be extracted and reprocessed, though bear in mind some
Expand All @@ -95,7 +95,7 @@ pub static ARGS: [ArgInfo<gcc::ArgData>; 10] = [
take_arg!("-gcc-toolchain", OsString, Separated, PassThrough),
take_arg!("-include-pch", PathBuf, CanBeSeparated, PreprocessorArgumentPath),
take_arg!("-target", OsString, Separated, PassThrough),
];
]);

#[cfg(test)]
mod test {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ArgData!{ pub
use self::ArgData::*;

// Mostly taken from https://github.com/ccache/ccache/blob/master/src/compopt.c#L32-L84
pub static ARGS: [ArgInfo<ArgData>; 65] = [
counted_array!(pub static ARGS: [ArgInfo<ArgData>; _] = [
flag!("-", TooHardFlag),
flag!("--coverage", Coverage),
take_arg!("--param", OsString, Separated, PassThrough),
Expand Down Expand Up @@ -163,7 +163,7 @@ pub static ARGS: [ArgInfo<ArgData>; 65] = [
take_arg!("-u", OsString, CanBeSeparated, PassThrough),
take_arg!("-x", OsString, CanBeSeparated, Language),
take_arg!("@", OsString, Concatenated, TooHard),
];
]);

/// Parse `arguments`, determining whether it is supported.
///
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ ArgData!{

use self::ArgData::*;

static ARGS: [ArgInfo<ArgData>; 23] = [
counted_array!(static ARGS: [ArgInfo<ArgData>; _] = [
take_arg!("-D", OsString, Concatenated, PreprocessorArgument),
take_arg!("-FA", OsString, Concatenated, TooHard),
take_arg!("-FI", PathBuf, CanBeSeparated, PreprocessorArgumentPath),
Expand All @@ -243,7 +243,7 @@ static ARGS: [ArgInfo<ArgData>; 23] = [
take_arg!("-o", PathBuf, Separated, Output), // Deprecated but valid
flag!("-showIncludes", ShowIncludes),
take_arg!("@", PathBuf, Concatenated, TooHardPath),
];
]);

pub fn parse_arguments(arguments: &[OsString], cwd: &Path, is_clang: bool) -> CompilerArguments<ParsedArguments> {
let mut output_arg = None;
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ ArgData!{
use self::ArgData::*;

// These are taken from https://github.com/rust-lang/rust/blob/b671c32ddc8c36d50866428d83b7716233356721/src/librustc/session/config.rs#L1186
static ARGS: [ArgInfo<ArgData>; 33] = [
counted_array!(static ARGS: [ArgInfo<ArgData>; _] = [
flag!("-", TooHardFlag),
take_arg!("--allow", OsString, CanBeSeparated('='), PassThrough),
take_arg!("--cap-lints", OsString, CanBeSeparated('='), PassThrough),
Expand Down Expand Up @@ -610,7 +610,7 @@ static ARGS: [ArgInfo<ArgData>; 33] = [
take_arg!("-Z", OsString, CanBeSeparated, PassThrough),
take_arg!("-l", ArgLinkLibrary, CanBeSeparated, LinkLibrary),
take_arg!("-o", PathBuf, CanBeSeparated, TooHardPath),
];
]);

fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<ParsedArguments>
{
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extern crate bytes;
extern crate chrono;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate counted_array;
#[cfg(feature = "rust-crypto")]
extern crate crypto;
#[cfg(unix)]
Expand Down

0 comments on commit aeedd7e

Please sign in to comment.