diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-05-11 13:06:16 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-05-17 10:30:01 +0200 |
commit | df0db108c06836bd986a091274e027838523c23d (patch) | |
tree | e10d4de89ad802ea57cc62f9f14854d45dce2c6d /src/commands | |
parent | 0cf614e8ed40bd0e7cc2b3443b68abe59659c46e (diff) |
Refactor: Return Cow instead of String
This results in less overhead in the or-else case by now allocating a
String object.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/endpoint_container.rs | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/commands/endpoint_container.rs b/src/commands/endpoint_container.rs index 9ee3f3b..082749b 100644 --- a/src/commands/endpoint_container.rs +++ b/src/commands/endpoint_container.rs @@ -10,6 +10,7 @@ //! Implementation of the 'endpoint container' subcommand +use std::borrow::Cow; use std::str::FromStr; use anyhow::Error; @@ -183,16 +184,27 @@ async fn inspect(container: Container<'_>) -> Result<()> { let d = container.inspect().await?; - fn option_vec(ov: Option<&Vec<String>>) -> String { - ov.map(|v| format!("Some({})", v.iter().join(", "))).unwrap_or_else(|| String::from("None")) + fn option_vec<'a>(ov: Option<&Vec<String>>) -> Cow<'a, str> { + ov.map(|v| format!("Some({})", v.iter().join(", "))) + .map(Cow::from) + .unwrap_or_else(|| Cow::from("None")) } - fn option_vec_nl(ov: Option<&Vec<String>>, ind: usize) -> String { - ov.map(|v| v.iter().map(|s| format!("{:ind$}{s}", "", ind = ind, s = s)).join("\n")).map(|s| format!("\n{}", s)).unwrap_or_else(|| String::from("None")) + fn option_vec_nl<'a>(ov: Option<&Vec<String>>, ind: usize) -> Cow<'a, str> { + ov.map(|v| { + v.iter() + .map(|s| format!("{:ind$}{s}", "", ind = ind, s = s)) + .join("\n") + }) + .map(|s| format!("\n{}", s)) + .map(Cow::from) + .unwrap_or_else(|| Cow::from("None")) } - fn option_tostr<T: ToString>(ots: Option<T>) -> String { - ots.map(|s| format!("Some({})", s.to_string())).unwrap_or_else(|| String::from("None")) + fn option_tostr<'a, T: ToString + 'a>(ots: Option<T>) -> Cow<'a, str> { + ots.map(|s| format!("Some({})", s.to_string())) + .map(Cow::from) + .unwrap_or_else(|| Cow::from("None")) } writeln!(std::io::stdout(), "{}", indoc::formatdoc!(r#" |