nushell/crates/nu-command/src
Bahex 55de232a1c
refactor Value::follow_cell_path to reduce clones and return Cow (#15640)
# Description
While working on something else, I noticed that
`Value::follow_cell_path` receives `self`.

While it would be ideal for the signature to be `(&'a self, cell_path)
-> &'a Value`, that's not possible because:
1. Selecting a row from a list and field from a record can be done with
a reference but selecting a column from a table requires creating a new
list.
2. `Value::Custom` returns new `Value`s when indexed.

So the signature becomes `(&'a self, cell_path) -> Cow<'a, Value>`.

Another complication that arises is, once a new `Value` is created, and
it is further indexed, the `current` variable
1. can't be `&'a Value`, as the lifetime requirement means it can't
refer to local variables
2. _shouldn't_ be `Cow<'a, Value>`, as once it becomes an owned value,
it can't be borrowed ever again, as `current` is derived from its
previous value in further iterations. So once it's owned, it can't be
indexed by reference, leading to more clones

We need `current` to have _two_ possible lifetimes
1. `'out`: references derived from `&self`
2. `'local`: references derived from an owned value stored in a local
variable

```rust
enum MultiLife<'out, 'local, T>
where
    'out: 'local,
    T: ?Sized,
{
    Out(&'out T),
    Local(&'local T),
}
```
With `current: MultiLife<'out, '_, Value>`, we can traverse values with
minimal clones, and we can transform it to `Cow<'out, Value>` easily
(`MultiLife::Out -> Cow::Borrowed, MultiLife::Local -> Cow::Owned`) to
return it

# User-Facing Changes

# Tests + Formatting

# After Submitting

---------

Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com>
2025-05-01 09:43:57 -05:00
..
bytes fix range bugs in str substring, str index-of, slice, bytes at (#14863) 2025-01-30 06:50:01 -06:00
charting Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
conversions feat: duration from record (#15600) 2025-04-19 18:29:12 -05:00
database Refactor I/O Errors (#14927) 2025-01-28 16:03:31 -06:00
date Fix examples about RFC3339 format in date now and format date. (#15563) 2025-04-26 19:06:08 -05:00
debug add --raw-value option to debug command (#15581) 2025-04-17 12:12:07 -05:00
env Add job tags (#15555) 2025-04-21 20:08:00 +08:00
experimental Update job_recv.rs (#15673) 2025-05-01 06:19:32 -05:00
filesystem Add cat and get-content to open's search terms (#15643) 2025-04-25 06:56:30 -05:00
filters refactor Value::follow_cell_path to reduce clones and return Cow (#15640) 2025-05-01 09:43:57 -05:00
formats Fix #14660: to md breaks on tables with empty values (#15631) 2025-04-24 09:09:48 -05:00
generators seq date: generalize to allow any duration for --increment argument (#14903) 2025-01-25 13:24:39 -06:00
hash add binary as input to hash commands (#13923) 2024-09-25 16:47:52 +08:00
help Rework operator type errors (#14429) 2025-02-12 20:03:40 -08:00
math Fix clippy (#15489) 2025-04-06 09:49:28 +08:00
misc More precise ErrorKind::NotFound errors (#15149) 2025-02-22 11:42:44 -05:00
network change http get header example to use a record (#15674) 2025-05-01 09:42:53 -05:00
path Replace some PipelineMismatch by OnlySupportsThisInputType by shell error (#15447) 2025-04-07 12:25:27 +02:00
platform refactor Value::follow_cell_path to reduce clones and return Cow (#15640) 2025-05-01 09:43:57 -05:00
random Update rand and rand_chacha to 0.9 (#15463) 2025-04-01 07:15:39 -05:00
removed refactor: rename subcommand structs (#15309) 2025-03-14 02:00:35 +01:00
shells Jobs (#14883) 2025-02-25 12:09:52 -05:00
stor Run-time pipeline input typechecking tweaks (#14922) 2025-02-02 15:51:47 -05:00
strings Fix examples about RFC3339 format in date now and format date. (#15563) 2025-04-26 19:06:08 -05:00
system Inter-Job direct messaging (#15253) 2025-04-26 23:24:35 +08:00
viewers nu-table/ 1 refactoring + a few optimizations + small fix (#15653) 2025-05-01 09:43:30 -05:00
default_context.rs Inter-Job direct messaging (#15253) 2025-04-26 23:24:35 +08:00
example_test.rs Implementing ByteStream interuption on infinite stream (#13552) 2025-01-11 13:28:08 -08:00
lib.rs Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
progress_bar.rs Cut down unnecessary lint allows (#14335) 2024-11-15 19:24:39 +01:00
sort_utils.rs refactor Value::follow_cell_path to reduce clones and return Cow (#15640) 2025-05-01 09:43:57 -05:00