Skip to content

Commit

Permalink
Reformat Rust code (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlopko committed May 27, 2021
1 parent f079995 commit 7458499
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 43 deletions.
6 changes: 4 additions & 2 deletions examples/cargo_manifest_dir/external_crate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

pub fn get_included_str() -> &'static str {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/include/included_file.rs.inc"))
include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/include/included_file.rs.inc"
))
}
2 changes: 1 addition & 1 deletion examples/crate_universe/uses_proc_macro/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use structopt::StructOpt;
#[derive(StructOpt)]
struct Opt {
#[structopt(long)]
name: String
name: String,
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/fibonacci/benches/fibonacci_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#![feature(test)]

extern crate test;
extern crate fibonacci;
extern crate test;

use test::Bencher;

Expand Down
2 changes: 1 addition & 1 deletion examples/per_platform_printer/print_windows.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub fn print() -> String {
"Hello Windows!".to_owned()
"Hello Windows!".to_owned()
}
10 changes: 8 additions & 2 deletions test/build_env/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
fn main() {
println!("cargo:rustc-env=CARGO_PKG_NAME_FROM_BUILD_SCRIPT={}", env!("CARGO_PKG_NAME"));
println!("cargo:rustc-env=CARGO_CRATE_NAME_FROM_BUILD_SCRIPT={}", env!("CARGO_CRATE_NAME"));
println!(
"cargo:rustc-env=CARGO_PKG_NAME_FROM_BUILD_SCRIPT={}",
env!("CARGO_PKG_NAME")
);
println!(
"cargo:rustc-env=CARGO_CRATE_NAME_FROM_BUILD_SCRIPT={}",
env!("CARGO_CRATE_NAME")
);
println!("cargo:rustc-env=HAS_TRAILING_SLASH=foo\\");
}
10 changes: 8 additions & 2 deletions test/build_env/tests/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
fn cargo_env_vars() {
assert_eq!(env!("CARGO_PKG_NAME"), "cargo_env-vars_test");
assert_eq!(env!("CARGO_CRATE_NAME"), "cargo_env_vars_test");
assert_eq!(env!("CARGO_PKG_NAME_FROM_BUILD_SCRIPT"), "cargo_build_script_env-vars");
assert_eq!(env!("CARGO_CRATE_NAME_FROM_BUILD_SCRIPT"), "cargo_build_script_env_vars");
assert_eq!(
env!("CARGO_PKG_NAME_FROM_BUILD_SCRIPT"),
"cargo_build_script_env-vars"
);
assert_eq!(
env!("CARGO_CRATE_NAME_FROM_BUILD_SCRIPT"),
"cargo_build_script_env_vars"
);
assert_eq!(env!("HAS_TRAILING_SLASH"), "foo\\");
}
5 changes: 4 additions & 1 deletion test/build_env/tests/custom_crate_name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[test]
fn cargo_env_vars() {
assert_eq!(env!("CARGO_PKG_NAME"), "cargo-env-vars-custom-crate-name-test");
assert_eq!(
env!("CARGO_PKG_NAME"),
"cargo-env-vars-custom-crate-name-test"
);
assert_eq!(env!("CARGO_CRATE_NAME"), "custom_crate_name");
}
5 changes: 4 additions & 1 deletion test/build_env/tests/manifest_dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[test]
pub fn test_manifest_dir() {
let actual = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/manifest_dir_file.txt"));
let actual = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/manifest_dir_file.txt"
));
let expected = "This file tests that CARGO_MANIFEST_DIR is set for the build environment\n";
assert_eq!(actual, expected);
}
2 changes: 1 addition & 1 deletion test/inline_test_with_deps/test_with_srcs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod extra;

#[cfg(test)]
mod tests {
use super::{multiply, extra};
use super::{extra, multiply};
use dep::example_test_dep_fn;

#[test]
Expand Down
17 changes: 12 additions & 5 deletions test/test_env/tests/run.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
#[test]
fn run() {
let path = env!("CARGO_BIN_EXE_hello-world");
let output = std::process::Command::new(path).output().expect("Failed to run process");
let output = std::process::Command::new(path)
.output()
.expect("Failed to run process");
assert_eq!(&b"Hello world\n"[..], output.stdout.as_slice());

// Test the `env` attribute of `rust_test` at run time
assert_eq!(std::env::var("FERRIS_SAYS").unwrap(), "Hello fellow Rustaceans!");
assert_eq!(
std::env::var("FERRIS_SAYS").unwrap(),
"Hello fellow Rustaceans!"
);

// Test the behavior of `rootpath` and that a binary can be found relative to current_dir
let hello_world_bin = std::path::PathBuf::from(std::env::var_os("HELLO_WORLD_BIN_ROOTPATH").unwrap());
let hello_world_bin =
std::path::PathBuf::from(std::env::var_os("HELLO_WORLD_BIN_ROOTPATH").unwrap());
assert_eq!(
hello_world_bin.as_path(),
hello_world_bin.as_path(),
std::path::Path::new("test/test_env/hello-world"),
);
assert!(!hello_world_bin.is_absolute());
assert!(hello_world_bin.exists());

// Ensure `execpath` expanded variables map to real files and have absolute paths
let hello_world_src = std::path::PathBuf::from(std::env::var("HELLO_WORLD_SRC_EXECPATH").unwrap());
let hello_world_src =
std::path::PathBuf::from(std::env::var("HELLO_WORLD_SRC_EXECPATH").unwrap());
assert!(hello_world_src.is_absolute());
assert!(hello_world_src.exists());
}
2 changes: 1 addition & 1 deletion test/transitive_lib/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
println!("cargo:rustc-link-lib=alias:shell32");
println!("cargo:rustc-link-lib=alias:shell32");
}
2 changes: 1 addition & 1 deletion test/unit/cc_info/bin_using_native_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ extern "C" {
}
fn main() {
println!("{}", unsafe { native_dep() })
}
}
4 changes: 3 additions & 1 deletion test/unit/cc_info/foo.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub fn main() { dbg!(42); }
pub fn main() {
dbg!(42);
}
2 changes: 1 addition & 1 deletion test/unit/cc_info/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ extern "C" {
#[proc_macro_derive(UsingNativeDep)]
pub fn use_native_dep(_input: TokenStream) -> TokenStream {
panic!("done")
}
}
1 change: 1 addition & 0 deletions test/unit/crate_name/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions test/unit/interleaved_cc_info/a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions test/unit/interleaved_cc_info/c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions test/unit/interleaved_cc_info/e.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion test/unit/native_deps/bin_using_native_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ extern "C" {
}
fn main() {
println!("{}", unsafe { native_dep() })
}
}
2 changes: 1 addition & 1 deletion test/unit/native_deps/lib_using_native_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ extern "C" {
}
pub fn use_native_dep() {
println!("{}", unsafe { native_dep() })
}
}
2 changes: 1 addition & 1 deletion test/unit/native_deps/proc_macro_using_native_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ extern "C" {
pub fn use_native_dep(_input: TokenStream) -> TokenStream {
println!("{}", unsafe { native_dep() });
panic!("done")
}
}
5 changes: 2 additions & 3 deletions test/versioned_dylib/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

extern crate libc;

extern {
extern "C" {
pub fn return_zero() -> libc::c_int;
}

Expand All @@ -23,10 +23,9 @@ fn main() {
println!("Got {} from our shared lib", zero);
}


#[cfg(test)]
mod test {
extern {
extern "C" {
pub fn return_zero() -> libc::c_int;
}

Expand Down
3 changes: 2 additions & 1 deletion util/label/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ mod tests {
assert_eq!(
analyze("foo//bar"),
Err(LabelError(
"foo//bar must be a legal label; '//' cannot appear in the middle of the label.".to_string()
"foo//bar must be a legal label; '//' cannot appear in the middle of the label."
.to_string()
))
);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion util/label/label_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub struct LabelError(pub String);

impl std::fmt::Display for LabelError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f,"{}",self.0)
write!(f, "{}", self.0)
}
}

Expand Down
31 changes: 17 additions & 14 deletions util/launcher/launcher_main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::Command;
use std::vec::Vec;
Expand All @@ -11,7 +11,7 @@ use std::os::unix::process::CommandExt;
/// This string must match the one found in `_create_test_launcher`
const LAUNCHFILES_ENV_PATH: &'static str = ".launchfiles/env";

/// Load environment variables from a uniquly formatted
/// Load environment variables from a uniquly formatted
fn environ() -> BTreeMap<String, String> {
let mut environ = BTreeMap::new();

Expand All @@ -21,7 +21,7 @@ fn environ() -> BTreeMap<String, String> {
let env_path = std::env::args().nth(0).expect("arg 0 was not set") + LAUNCHFILES_ENV_PATH;
let file = File::open(env_path).expect("Failed to load the environment file");

// Variables will have the `${pwd}` variable replaced which is rendered by
// Variables will have the `${pwd}` variable replaced which is rendered by
// `@rules_rust//rust/private:util.bzl::expand_locations`
let pwd = std::env::current_dir().expect("Failed to get current working directory");
let pwd_str = pwd.to_string_lossy();
Expand All @@ -35,25 +35,28 @@ fn environ() -> BTreeMap<String, String> {

environ.insert(
key.expect("Key is not set"),
line.expect("Failed to read line").replace("${pwd}", &pwd_str),
line.expect("Failed to read line")
.replace("${pwd}", &pwd_str),
);

key = None;
}

environ
}

/// Locate the executable based on the name of the launcher executable
fn executable() -> PathBuf {
let mut exec_path = std::env::args().nth(0).expect("arg 0 was not set");
let stem_index = exec_path.rfind(".launcher").expect("This executable should always contain `.launcher`");
let stem_index = exec_path
.rfind(".launcher")
.expect("This executable should always contain `.launcher`");

// Remove the substring from the exec path
for _char in ".launcher".chars() {
exec_path.remove(stem_index);
}

PathBuf::from(exec_path)
}

Expand All @@ -67,9 +70,9 @@ fn args() -> Vec<String> {
#[cfg(target_family = "unix")]
fn exec(environ: BTreeMap<String, String>, executable: PathBuf, args: Vec<String>) {
let error = Command::new(&executable)
.envs(environ.iter())
.args(args)
.exec();
.envs(environ.iter())
.args(args)
.exec();

panic!("Process failed to start: {:?} with {:?}", executable, error)
}
Expand All @@ -79,10 +82,10 @@ fn exec(environ: BTreeMap<String, String>, executable: PathBuf, args: Vec<String
#[cfg(target_family = "windows")]
fn exec(environ: BTreeMap<String, String>, executable: PathBuf, args: Vec<String>) {
let output = Command::new(executable)
.envs(environ.iter())
.args(args)
.output()
.expect("Failed to run process");
.envs(environ.iter())
.args(args)
.output()
.expect("Failed to run process");

std::process::exit(output.status.code().unwrap_or(1));
}
Expand Down

0 comments on commit 7458499

Please sign in to comment.