mirror of
https://github.com/nushell/nushell.git
synced 2025-08-12 23:17:59 +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:
@ -262,29 +262,29 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Result<PathBuf, Shel
|
||||
|
||||
#[cfg(windows)]
|
||||
if let Some(val) = record.get("prefix") {
|
||||
let p = val.coerce_string()?;
|
||||
let p = val.coerce_str()?;
|
||||
if !p.is_empty() {
|
||||
result.push(p);
|
||||
result.push(p.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(val) = record.get("parent") {
|
||||
let p = val.coerce_string()?;
|
||||
let p = val.coerce_str()?;
|
||||
if !p.is_empty() {
|
||||
result.push(p);
|
||||
result.push(p.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
let mut basename = String::new();
|
||||
if let Some(val) = record.get("stem") {
|
||||
let p = val.coerce_string()?;
|
||||
let p = val.coerce_str()?;
|
||||
if !p.is_empty() {
|
||||
basename.push_str(&p);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(val) = record.get("extension") {
|
||||
let p = val.coerce_string()?;
|
||||
let p = val.coerce_str()?;
|
||||
if !p.is_empty() {
|
||||
basename.push('.');
|
||||
basename.push_str(&p);
|
||||
|
Reference in New Issue
Block a user