From ae0d4e8d2d4af36cc8959ef313bf1d0054b510f6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 Jan 2021 16:29:10 +0100 Subject: ContainerOptionsBuilder::env() should not get owned Vec (#237) This type signature makes it impossible to dynamically construct a Vec of environment variables and pass it to the builder. This patch tries to fix that by making the function only take a reference to a slice containing the environments. Signed-off-by: Matthias Beyer --- src/builder.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index c14cbb7..eba3536 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -854,10 +854,13 @@ impl ContainerOptionsBuilder { self } - pub fn env( + pub fn env( &mut self, - envs: Vec<&str>, - ) -> &mut Self { + envs: E, + ) -> &mut Self + where S: AsRef + Serialize, + E: AsRef<[S]> + Serialize + { self.params.insert("Env", json!(envs)); self } @@ -1697,6 +1700,23 @@ mod tests { ); } + #[test] + fn container_options_env_dynamic() { + let env: Vec = ["foo", "bar", "baz"] + .iter() + .map(|s| String::from(*s)) + .collect(); + + let options = ContainerOptionsBuilder::new("test_image") + .env(&env) + .build(); + + assert_eq!( + r#"{"Env":["foo","bar","baz"],"HostConfig":{},"Image":"test_image"}"#, + options.serialize().unwrap() + ); + } + #[test] fn container_options_user() { let options = ContainerOptionsBuilder::new("test_image") -- cgit v1.2.3