From: Richard Dodd Date: Wed, 4 Oct 2017 17:03:08 +0000 (+0100) Subject: Refactor Config::configure to take &mut. X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~22^2~6^2~15^2~2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=89c09e20c662524722278d13be4176b8f5a6af0e;p=cargo.git Refactor Config::configure to take &mut. --- diff --git a/src/bin/bench.rs b/src/bin/bench.rs index cb39fc9ce..1aa82dd9f 100644 --- a/src/bin/bench.rs +++ b/src/bin/bench.rs @@ -95,7 +95,7 @@ not affect how many jobs are used when running the benchmarks. Compilation can be customized with the `bench` profile in the manifest. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-bench; args={:?}", env::args().collect::>()); diff --git a/src/bin/build.rs b/src/bin/build.rs index 89e63b517..883e30db6 100644 --- a/src/bin/build.rs +++ b/src/bin/build.rs @@ -87,7 +87,7 @@ the manifest. The default profile for this command is `dev`, but passing the --release flag will use the `release` profile instead. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-build; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 48c2a1347..e92bcdbe5 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -75,7 +75,7 @@ See 'cargo help ' for more information on a specific command. fn main() { env_logger::init().unwrap(); - let config = match Config::default() { + let mut config = match Config::default() { Ok(cfg) => cfg, Err(e) => { let mut shell = Shell::new(); @@ -92,7 +92,7 @@ fn main() { }) .collect()); let rest = &args; - cargo::call_main_without_stdin(execute, &config, USAGE, rest, true) + cargo::call_main_without_stdin(execute, &mut config, USAGE, rest, true) })(); match result { @@ -146,7 +146,7 @@ each_subcommand!(declare_mod); because they are fundamental (and intertwined). Other commands can rely on this top-level information. */ -fn execute(flags: Flags, config: &Config) -> CliResult { +fn execute(flags: Flags, config: &mut Config) -> CliResult { config.configure(flags.flag_verbose, flags.flag_quiet, &flags.flag_color, @@ -250,11 +250,12 @@ fn execute(flags: Flags, config: &Config) -> CliResult { execute_external_subcommand(config, &args[1], &args) } -fn try_execute_builtin_command(config: &Config, args: &[String]) -> Option { +fn try_execute_builtin_command(config: &mut Config, args: &[String]) -> Option { macro_rules! cmd { ($name:ident) => (if args[1] == stringify!($name).replace("_", "-") { config.shell().set_verbosity(Verbosity::Verbose); - let r = cargo::call_main_without_stdin($name::execute, config, + let r = cargo::call_main_without_stdin($name::execute, + config, $name::USAGE, &args, false); diff --git a/src/bin/check.rs b/src/bin/check.rs index 488071d62..982204130 100644 --- a/src/bin/check.rs +++ b/src/bin/check.rs @@ -87,7 +87,7 @@ pub struct Options { flag_z: Vec, } -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-check; args={:?}", env::args().collect::>()); diff --git a/src/bin/clean.rs b/src/bin/clean.rs index 9e331dd64..446b5e502 100644 --- a/src/bin/clean.rs +++ b/src/bin/clean.rs @@ -45,7 +45,7 @@ given, then all packages' artifacts are removed. For more information on SPEC and its format, see the `cargo help pkgid` command. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-clean; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, diff --git a/src/bin/doc.rs b/src/bin/doc.rs index 836b10ca6..6e1783696 100644 --- a/src/bin/doc.rs +++ b/src/bin/doc.rs @@ -74,7 +74,7 @@ current package is documented. For more information on SPEC and its format, see the `cargo help pkgid` command. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-check; args={:?}", env::args().collect::>()); diff --git a/src/bin/fetch.rs b/src/bin/fetch.rs index 8dce4633f..880b77eb6 100644 --- a/src/bin/fetch.rs +++ b/src/bin/fetch.rs @@ -41,7 +41,7 @@ If the lockfile is not available, then this is the equivalent of all updated. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/generate_lockfile.rs b/src/bin/generate_lockfile.rs index f6733768c..11cc83639 100644 --- a/src/bin/generate_lockfile.rs +++ b/src/bin/generate_lockfile.rs @@ -34,7 +34,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-generate-lockfile; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, diff --git a/src/bin/git_checkout.rs b/src/bin/git_checkout.rs index ed020c7ef..ec3ae7a5b 100644 --- a/src/bin/git_checkout.rs +++ b/src/bin/git_checkout.rs @@ -32,7 +32,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/help.rs b/src/bin/help.rs index c5059e4ff..f7f564ee7 100644 --- a/src/bin/help.rs +++ b/src/bin/help.rs @@ -14,7 +14,7 @@ Options: -h, --help Print this message "; -pub fn execute(_: Options, _: &Config) -> CliResult { +pub fn execute(_: Options, _: &mut Config) -> CliResult { // This is a dummy command just so that `cargo help help` works. // The actual delegation of help flag to subcommands is handled by the // cargo command. diff --git a/src/bin/init.rs b/src/bin/init.rs index 29319e181..9252ddd39 100644 --- a/src/bin/init.rs +++ b/src/bin/init.rs @@ -43,7 +43,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-init; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, diff --git a/src/bin/install.rs b/src/bin/install.rs index 4fedab896..c7062d40c 100644 --- a/src/bin/install.rs +++ b/src/bin/install.rs @@ -101,7 +101,7 @@ the more explicit `install --path .`. The `--list` option will list all installed packages (and their versions). "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/locate_project.rs b/src/bin/locate_project.rs index e0c6164e7..6e16cca2d 100644 --- a/src/bin/locate_project.rs +++ b/src/bin/locate_project.rs @@ -23,8 +23,7 @@ pub struct ProjectLocation { root: String } -pub fn execute(flags: LocateProjectFlags, - config: &Config) -> CliResult { +pub fn execute(flags: LocateProjectFlags, config: &mut Config) -> CliResult { let root = find_root_manifest_for_wd(flags.flag_manifest_path, config.cwd())?; let string = root.to_str() diff --git a/src/bin/login.rs b/src/bin/login.rs index c41118cc3..99ce8e755 100644 --- a/src/bin/login.rs +++ b/src/bin/login.rs @@ -37,7 +37,7 @@ Options: "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/metadata.rs b/src/bin/metadata.rs index 9d52d610b..d10fe8c44 100644 --- a/src/bin/metadata.rs +++ b/src/bin/metadata.rs @@ -46,7 +46,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/new.rs b/src/bin/new.rs index dc0878d19..c006fd1e4 100644 --- a/src/bin/new.rs +++ b/src/bin/new.rs @@ -43,7 +43,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-new; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, diff --git a/src/bin/owner.rs b/src/bin/owner.rs index 438bb109d..6c76a6faf 100644 --- a/src/bin/owner.rs +++ b/src/bin/owner.rs @@ -47,7 +47,7 @@ See http://doc.crates.io/crates-io.html#cargo-owner for detailed documentation and troubleshooting. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/package.rs b/src/bin/package.rs index 06fc589bd..31e3330ad 100644 --- a/src/bin/package.rs +++ b/src/bin/package.rs @@ -44,7 +44,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/pkgid.rs b/src/bin/pkgid.rs index d69c982df..01f8a8f6b 100644 --- a/src/bin/pkgid.rs +++ b/src/bin/pkgid.rs @@ -55,8 +55,7 @@ Example Package IDs "; -pub fn execute(options: Options, - config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/publish.rs b/src/bin/publish.rs index 0ce5d897f..c34a0e270 100644 --- a/src/bin/publish.rs +++ b/src/bin/publish.rs @@ -49,7 +49,7 @@ Options: "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/read_manifest.rs b/src/bin/read_manifest.rs index 651174e87..eee2210d8 100644 --- a/src/bin/read_manifest.rs +++ b/src/bin/read_manifest.rs @@ -26,7 +26,7 @@ Options: --color WHEN Coloring: auto, always, never "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-read-manifest; args={:?}", env::args().collect::>()); config.shell().set_color_choice(options.flag_color.as_ref().map(|s| &s[..]))?; diff --git a/src/bin/run.rs b/src/bin/run.rs index d6bee1f09..21486baf6 100644 --- a/src/bin/run.rs +++ b/src/bin/run.rs @@ -64,7 +64,7 @@ arguments to both Cargo and the binary, the ones after `--` go to the binary, the ones before go to Cargo. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/rustc.rs b/src/bin/rustc.rs index fafa8632d..e6f5dc540 100644 --- a/src/bin/rustc.rs +++ b/src/bin/rustc.rs @@ -86,7 +86,7 @@ processes spawned by Cargo, use the $RUSTFLAGS environment variable or the `build.rustflags` configuration option. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-rustc; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, diff --git a/src/bin/rustdoc.rs b/src/bin/rustdoc.rs index 6ff12689c..156a6b867 100644 --- a/src/bin/rustdoc.rs +++ b/src/bin/rustdoc.rs @@ -83,7 +83,7 @@ current package is documented. For more information on SPEC and its format, see the `cargo help pkgid` command. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/search.rs b/src/bin/search.rs index 5d5846e85..165dea1c8 100644 --- a/src/bin/search.rs +++ b/src/bin/search.rs @@ -38,7 +38,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/test.rs b/src/bin/test.rs index 7e30e38e6..2208a18af 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -116,7 +116,7 @@ To get the list of all options available for the test binaries use this: cargo test -- --help "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-test; args={:?}", env::args().collect::>()); diff --git a/src/bin/uninstall.rs b/src/bin/uninstall.rs index 69079265e..2adf2d041 100644 --- a/src/bin/uninstall.rs +++ b/src/bin/uninstall.rs @@ -40,7 +40,7 @@ uninstalled for a crate but the `--bin` and `--example` flags can be used to only uninstall particular binaries. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/bin/update.rs b/src/bin/update.rs index a9a57312e..1e7f92b53 100644 --- a/src/bin/update.rs +++ b/src/bin/update.rs @@ -60,7 +60,7 @@ updated. For more information about package id specifications, see `cargo help pkgid`. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { debug!("executing; cmd=cargo-update; args={:?}", env::args().collect::>()); config.configure(options.flag_verbose, options.flag_quiet, diff --git a/src/bin/verify_project.rs b/src/bin/verify_project.rs index 208905671..a05447026 100644 --- a/src/bin/verify_project.rs +++ b/src/bin/verify_project.rs @@ -39,7 +39,7 @@ Options: -Z FLAG ... Unstable (nightly-only) flags to Cargo "; -pub fn execute(args: Flags, config: &Config) -> CliResult { +pub fn execute(args: Flags, config: &mut Config) -> CliResult { config.configure(args.flag_verbose, args.flag_quiet, &args.flag_color, diff --git a/src/bin/version.rs b/src/bin/version.rs index f036def03..6d3772f1c 100644 --- a/src/bin/version.rs +++ b/src/bin/version.rs @@ -18,7 +18,7 @@ Options: --color WHEN Coloring: auto, always, never "; -pub fn execute(_: Options, _: &Config) -> CliResult { +pub fn execute(_: Options, _: &mut Config) -> CliResult { debug!("executing; cmd=cargo-version; args={:?}", env::args().collect::>()); println!("{}", cargo::version()); diff --git a/src/bin/yank.rs b/src/bin/yank.rs index 0526c4c4f..a00892a51 100644 --- a/src/bin/yank.rs +++ b/src/bin/yank.rs @@ -45,7 +45,7 @@ download the yanked version to use it. Cargo will, however, not allow any new crates to be locked to any yanked version. "; -pub fn execute(options: Options, config: &Config) -> CliResult { +pub fn execute(options: Options, config: &mut Config) -> CliResult { config.configure(options.flag_verbose, options.flag_quiet, &options.flag_color, diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 3ecc7b1ad..f20118b80 100755 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -109,8 +109,8 @@ impl fmt::Display for VersionInfo { } pub fn call_main_without_stdin<'de, Flags: Deserialize<'de>>( - exec: fn(Flags, &Config) -> CliResult, - config: &Config, + exec: fn(Flags, &mut Config) -> CliResult, + config: &mut Config, usage: &str, args: &[String], options_first: bool) -> CliResult diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 38e2e303f..c2316a960 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -107,37 +107,46 @@ impl Config { Ok(Config::new(shell, cwd, homedir)) } + /// The user's cargo home directory (OS-dependent) pub fn home(&self) -> &Filesystem { &self.home_path } + /// The cargo git directory (`/git`) pub fn git_path(&self) -> Filesystem { self.home_path.join("git") } + /// The cargo registry index directory (`/registry/index`) pub fn registry_index_path(&self) -> Filesystem { self.home_path.join("registry").join("index") } + /// The cargo registry cache directory (`/registry/path`) pub fn registry_cache_path(&self) -> Filesystem { self.home_path.join("registry").join("cache") } + /// The cargo registry source directory (`/registry/src`) pub fn registry_source_path(&self) -> Filesystem { self.home_path.join("registry").join("src") } + /// Get a reference to the shell, for e.g. writing error messages pub fn shell(&self) -> RefMut { self.shell.borrow_mut() } + /// Get the path to the `rustdoc` executable pub fn rustdoc(&self) -> CargoResult<&Path> { self.rustdoc.get_or_try_init(|| self.get_tool("rustdoc")).map(AsRef::as_ref) } + /// Get the path to the `rustc` executable pub fn rustc(&self) -> CargoResult<&Rustc> { self.rustc.get_or_try_init(|| Rustc::new(self.get_tool("rustc")?, self.maybe_get_tool("rustc_wrapper")?)) } + /// Get the path to the `cargo` executable pub fn cargo_exe(&self) -> CargoResult<&Path> { self.cargo_exe.get_or_try_init(|| env::current_exe().and_then(|path| path.canonicalize()) @@ -384,7 +393,7 @@ impl Config { }) } - pub fn configure(&self, + pub fn configure(&mut self, verbose: u32, quiet: Option, color: &Option, @@ -450,6 +459,7 @@ impl Config { !self.frozen.get() && !self.locked.get() } + /// Loads configuration from the filesystem pub fn load_values(&self) -> CargoResult> { let mut cfg = CV::Table(HashMap::new(), PathBuf::from(".")); @@ -483,6 +493,7 @@ impl Config { } } + /// Loads credentials config from the credentials file into the ConfigValue object, if present. fn load_credentials(&self, cfg: &mut ConfigValue) -> CargoResult<()> { let home_path = self.home_path.clone().into_path_unlocked(); let credentials = home_path.join("credentials"); @@ -516,6 +527,8 @@ impl Config { match (registry, value) { (&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => { + // Take ownership of `new` by swapping it with an empty hashmap, so we can move + // into an iterator. let new = mem::replace(new, HashMap::new()); for (key, value) in new { old.insert(key, value);