Skip to content

Commit afe83e0

Browse files
committed
WIP: add impl
1 parent c0cf508 commit afe83e0

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

src/env_var.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use crate::process;
12
use std::collections::HashSet;
23
use std::env;
34
use std::ffi::OsStr;
45
use std::path::PathBuf;
56
use std::process::Command;
6-
7-
use crate::process;
7+
use std::vec;
88

99
pub const RUST_RECURSION_COUNT_MAX: u32 = 20;
1010

@@ -13,14 +13,34 @@ trait ProcessEnvs {
1313
type Iter: Iterator<Item = Self::Item>;
1414

1515
fn get_envs(&self) -> Self::Iter;
16-
fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
16+
fn env<K, V>(&mut self, key: K, val: V)
1717
where
1818
K: AsRef<OsStr>,
1919
V: AsRef<OsStr>;
2020
}
2121

22+
impl ProcessEnvs for Command {
23+
type Item = Command;
24+
type Iter = vec::IntoIter<Command>;
25+
26+
fn get_envs(&self) -> <Command as ProcessEnvs>::Iter {
27+
self.get_envs()
28+
}
29+
fn env<K, V>(&mut self, key: K, val: V)
30+
where
31+
K: AsRef<OsStr>,
32+
V: AsRef<OsStr>,
33+
{
34+
self.env(key, val);
35+
}
36+
}
37+
2238
#[allow(unused)]
23-
pub fn append_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
39+
pub fn append_path(
40+
name: &str,
41+
value: Vec<PathBuf>,
42+
cmd: &mut dyn ProcessEnvs<Item = Command, Iter = vec::IntoIter<Command>>,
43+
) {
2444
let old_value = process().var_os(name);
2545
let mut parts: Vec<PathBuf>;
2646
if let Some(ref v) = old_value {
@@ -34,7 +54,11 @@ pub fn append_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
3454
}
3555
}
3656

37-
pub fn prepend_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
57+
pub fn prepend_path(
58+
name: &str,
59+
value: Vec<PathBuf>,
60+
cmd: &mut dyn ProcessEnvs<Item = Command, Iter = vec::IntoIter<Command>>,
61+
) {
3862
let old_value = process().var_os(name);
3963
let parts: Vec<PathBuf>;
4064
if let Some(ref v) = old_value {
@@ -97,9 +121,28 @@ mod tests {
97121
use crate::currentprocess;
98122
use crate::test::{with_saved_path, Env};
99123

124+
use super::ProcessEnvs;
100125
use std::collections::HashMap;
101126
use std::ffi::{OsStr, OsString};
102127

128+
struct TestCommand();
129+
130+
impl ProcessEnvs for TestCommand {
131+
type Item = Command;
132+
type Iter = vec::IntoIter<Command>;
133+
134+
fn get_envs(&self) -> <Command as ProcessEnvs>::Iter {
135+
self.get_envs()
136+
}
137+
fn env<K, V>(&mut self, key: K, val: V) -> &mut <Command as ProcessEnvs>::Item
138+
where
139+
K: AsRef<OsStr>,
140+
V: AsRef<OsStr>,
141+
{
142+
self.env(key, val)
143+
}
144+
}
145+
103146
#[test]
104147
fn deduplicate_and_concat_paths() {
105148
let mut old_paths = vec![];
@@ -177,7 +220,6 @@ mod tests {
177220
path_entries.push(path_z);
178221

179222
prepend_path("PATH", path_entries, &mut cmd);
180-
181223
let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
182224

183225
assert_eq!(

0 commit comments

Comments
 (0)