mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:25:38 +02:00
Add Value::coerce_str
(#11885)
# Description Following #11851, this PR adds one final conversion function for `Value`. `Value::coerce_str` takes a `&Value` and converts it to a `Cow<str>`, creating an owned `String` for types that needed converting. Otherwise, it returns a borrowed `str` for `String` and `Binary` `Value`s which avoids a clone/allocation. Where possible, `coerce_str` and `coerce_into_string` should be used instead of `coerce_string`, since `coerce_string` always allocates a new `String`.
This commit is contained in:
@ -110,10 +110,10 @@ pub fn env_to_string(
|
||||
Value::List { vals, .. } => {
|
||||
let paths = vals
|
||||
.iter()
|
||||
.map(|v| v.coerce_string())
|
||||
.map(Value::coerce_str)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
match std::env::join_paths(paths) {
|
||||
match std::env::join_paths(paths.iter().map(AsRef::as_ref)) {
|
||||
Ok(p) => Ok(p.to_string_lossy().to_string()),
|
||||
Err(_) => Err(ShellError::EnvVarNotAString {
|
||||
envvar_name: env_name.to_string(),
|
||||
|
Reference in New Issue
Block a user