From afe83e0c0fc194c3be4e492309dd38b5a1b95c99 Mon Sep 17 00:00:00 2001 From: chansuke Date: Thu, 4 Nov 2021 22:26:05 +0900 Subject: [PATCH] WIP: add impl --- src/env_var.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/src/env_var.rs b/src/env_var.rs index ea1799d1048..90a824791b2 100644 --- a/src/env_var.rs +++ b/src/env_var.rs @@ -1,10 +1,10 @@ +use crate::process; use std::collections::HashSet; use std::env; use std::ffi::OsStr; use std::path::PathBuf; use std::process::Command; - -use crate::process; +use std::vec; pub const RUST_RECURSION_COUNT_MAX: u32 = 20; @@ -13,14 +13,34 @@ trait ProcessEnvs { type Iter: Iterator; fn get_envs(&self) -> Self::Iter; - fn env(&mut self, key: K, val: V) -> &mut Self + fn env(&mut self, key: K, val: V) where K: AsRef, V: AsRef; } +impl ProcessEnvs for Command { + type Item = Command; + type Iter = vec::IntoIter; + + fn get_envs(&self) -> ::Iter { + self.get_envs() + } + fn env(&mut self, key: K, val: V) + where + K: AsRef, + V: AsRef, + { + self.env(key, val); + } +} + #[allow(unused)] -pub fn append_path(name: &str, value: Vec, cmd: &mut Command) { +pub fn append_path( + name: &str, + value: Vec, + cmd: &mut dyn ProcessEnvs>, +) { let old_value = process().var_os(name); let mut parts: Vec; if let Some(ref v) = old_value { @@ -34,7 +54,11 @@ pub fn append_path(name: &str, value: Vec, cmd: &mut Command) { } } -pub fn prepend_path(name: &str, value: Vec, cmd: &mut Command) { +pub fn prepend_path( + name: &str, + value: Vec, + cmd: &mut dyn ProcessEnvs>, +) { let old_value = process().var_os(name); let parts: Vec; if let Some(ref v) = old_value { @@ -97,9 +121,28 @@ mod tests { use crate::currentprocess; use crate::test::{with_saved_path, Env}; + use super::ProcessEnvs; use std::collections::HashMap; use std::ffi::{OsStr, OsString}; + struct TestCommand(); + + impl ProcessEnvs for TestCommand { + type Item = Command; + type Iter = vec::IntoIter; + + fn get_envs(&self) -> ::Iter { + self.get_envs() + } + fn env(&mut self, key: K, val: V) -> &mut ::Item + where + K: AsRef, + V: AsRef, + { + self.env(key, val) + } + } + #[test] fn deduplicate_and_concat_paths() { let mut old_paths = vec![]; @@ -177,7 +220,6 @@ mod tests { path_entries.push(path_z); prepend_path("PATH", path_entries, &mut cmd); - let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect(); assert_eq!(