Repository navigation
Name the Value conversion functions more clearly - #11851
Conversation
There is now `to_debug_string` instead (previously `debug_value`).
|
Thank you so much for tackling the weird coercions under a wrong name. Can we provide some recommendations on the plethora of |
I was able to remove yet another one of the |
sholderbach
left a comment
There was a problem hiding this comment.
Thanks for the comment improvements!
The into_ lifting may sometimes become a double edged sword if folks trying to make changes in the future may feel incentivised to clone the whole Value as it is now taken by value and the borrowcheck message will suggest that first. But that is a future skill issue :P
| /// - `Int` | ||
| /// - `Float` | ||
| /// - `String` | ||
| /// - `Binary` (only if valid utf-8) | ||
| /// - `Date` |
There was a problem hiding this comment.
Thanks for making this clear across the different methods!
Should we doctest this?
(preferrably probably the inverse but that would be unwieldy to read)
There was a problem hiding this comment.
It's quite a simple function so I'm not sure. Maybe the doctests could go in an # Examples section to make it more readable?
There was a problem hiding this comment.
I think the more unfortunate situation would be if someone adds another coercion and doesn't document it but that is kind of harder to write concisely. (Having just the given ones as one line asserts would be easy as examples)
There was a problem hiding this comment.
I was thinking of doing something like this:
/// ```
/// # use nu_protocol::{
/// # ast::{CellPath, RangeInclusion},
/// # engine::Closure,
/// # Range, Record, ShellError, Span, Value
/// # };
/// # use chrono::DateTime;
/// #
/// # let values: Vec<Value> = vec![
/// # Value::test_bool(false),
/// # Value::test_int(0),
/// # Value::test_filesize(0),
/// # Value::test_duration(0),
/// # Value::test_date(DateTime::UNIX_EPOCH.into()),
/// # Value::test_range(Range {
/// # from: Value::test_nothing(),
/// # incr: Value::test_nothing(),
/// # to: Value::test_nothing(),
/// # inclusion: RangeInclusion::Inclusive,
/// # }),
/// # Value::test_float(0.0),
/// # Value::test_string(String::new()),
/// # Value::test_record(Record::new()),
/// # // Value::test_lazy_record(),
/// # Value::test_list(Vec::new()),
/// # Value::test_block(0),
/// # Value::test_closure(Closure {
/// # block_id: 0,
/// # captures: Vec::new(),
/// # }),
/// # Value::test_nothing(),
/// # Value::error(
/// # ShellError::NushellFailed { msg: String::new() },
/// # Span::test_data(),
/// # ),
/// # Value::test_binary(Vec::new()),
/// # Value::test_cell_path(CellPath {
/// # members: Vec::new(),
/// # }),
/// # // Value::test_custom_value(0),
/// # ];
/// #
/// # for val in &values {
/// assert_eq!(matches!(val, Value::Float { .. } | Value::Int { .. }), val.coerce_float().is_ok())
/// # };
/// ```
But this would have to be copy-pasted for each function. Or, we can add a public function somewhere that returns a Vec<Value> containing one of each Value case (perhaps it could be #[doc(hidden)]).
There was a problem hiding this comment.
It's a mouthful 😄
(as we already have the Value::test_... in the general API it probably wouldn't be too bad to have a sample generating function)
but I don't want to block those changes on this. It will cause churn with a lot of PRs anyways.
|
Nice! Thanks for implementing the elegant doctests as well! |
# 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`.
# Description This PR renames the conversion functions on `Value` to be more consistent. It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions. The conversion functions on `Value` now come in a few forms: - `coerce_{type}` takes a `&Value` and attempts to convert the value to `type` (e.g., `i64` are converted to `f64`). This is the old behavior of some of the `as_{type}` functions -- these functions have simply been renamed to better reflect what they do. - The new `as_{type}` functions take a `&Value` and returns an `Ok` result only if the value is of `type` (no conversion is attempted). The returned value will be borrowed if `type` is non-`Copy`, otherwise an owned value is returned. - `into_{type}` exists for non-`Copy` types, but otherwise does not attempt conversion just like `as_type`. It takes an owned `Value` and always returns an owned result. - `coerce_into_{type}` has the same relationship with `coerce_{type}` as `into_{type}` does with `as_{type}`. - `to_{kind}_string`: conversion to different string formats (debug, abbreviated, etc.). Only two of the old string conversion functions were removed, the rest have been renamed only. - `to_{type}`: other conversion functions. Currently, only `to_path` exists. (And `to_string` through `Display`.) This table summaries the above: | Form | Cost | Input Ownership | Output Ownership | Converts `Value` case/`type` | | ---------------------------- | ----- | --------------- | ---------------- | -------- | | `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No | | `into_{type}` | Cheap | Owned | Owned | No | | `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes | | `coerce_into_{type}` | Cheap | Owned | Owned | Yes | | `to_{kind}_string` | Expensive | Borrowed | Owned | Yes | | `to_{type}` | Expensive | Borrowed | Owned | Yes | # User-Facing Changes Breaking API change for `Value` in `nu-protocol` which is exposed as part of the plugin API.
# Description Following nushell#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`.
# Description This PR renames the conversion functions on `Value` to be more consistent. It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions. The conversion functions on `Value` now come in a few forms: - `coerce_{type}` takes a `&Value` and attempts to convert the value to `type` (e.g., `i64` are converted to `f64`). This is the old behavior of some of the `as_{type}` functions -- these functions have simply been renamed to better reflect what they do. - The new `as_{type}` functions take a `&Value` and returns an `Ok` result only if the value is of `type` (no conversion is attempted). The returned value will be borrowed if `type` is non-`Copy`, otherwise an owned value is returned. - `into_{type}` exists for non-`Copy` types, but otherwise does not attempt conversion just like `as_type`. It takes an owned `Value` and always returns an owned result. - `coerce_into_{type}` has the same relationship with `coerce_{type}` as `into_{type}` does with `as_{type}`. - `to_{kind}_string`: conversion to different string formats (debug, abbreviated, etc.). Only two of the old string conversion functions were removed, the rest have been renamed only. - `to_{type}`: other conversion functions. Currently, only `to_path` exists. (And `to_string` through `Display`.) This table summaries the above: | Form | Cost | Input Ownership | Output Ownership | Converts `Value` case/`type` | | ---------------------------- | ----- | --------------- | ---------------- | -------- | | `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No | | `into_{type}` | Cheap | Owned | Owned | No | | `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes | | `coerce_into_{type}` | Cheap | Owned | Owned | Yes | | `to_{kind}_string` | Expensive | Borrowed | Owned | Yes | | `to_{type}` | Expensive | Borrowed | Owned | Yes | # User-Facing Changes Breaking API change for `Value` in `nu-protocol` which is exposed as part of the plugin API.
# Description Following nushell#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`.
Description
This PR renames the conversion functions on
Valueto be more consistent. It follows the Rust API guidelines for ad-hoc conversions. The conversion functions onValuenow come in a few forms:coerce_{type}takes a&Valueand attempts to convert the value totype(e.g.,i64are converted tof64). This is the old behavior of some of theas_{type}functions -- these functions have simply been renamed to better reflect what they do.as_{type}functions take a&Valueand returns anOkresult only if the value is oftype(no conversion is attempted). The returned value will be borrowed iftypeis non-Copy, otherwise an owned value is returned.into_{type}exists for non-Copytypes, but otherwise does not attempt conversion just likeas_type. It takes an ownedValueand always returns an owned result.coerce_into_{type}has the same relationship withcoerce_{type}asinto_{type}does withas_{type}.to_{kind}_string: conversion to different string formats (debug, abbreviated, etc.). Only two of the old string conversion functions were removed, the rest have been renamed only.to_{type}: other conversion functions. Currently, onlyto_pathexists. (Andto_stringthroughDisplay.)This table summaries the above:
Valuecase/typeas_{type}into_{type}coerce_{type}coerce_into_{type}to_{kind}_stringto_{type}User-Facing Changes
Breaking API change for
Valueinnu-protocolwhich is exposed as part of the plugin API.