refactor Value::follow_cell_path to reduce clones and return Cow#15640
refactor Value::follow_cell_path to reduce clones and return Cow#15640fdncred merged 3 commits intonushell:mainfrom
Value::follow_cell_path to reduce clones and return Cow#15640Conversation
|
I see the usage of |
|
It's completely internal to nushell/crates/nu-protocol/src/value/mod.rs Lines 2005 to 2009 in 2a2c1a3 A single level member access is done with this function. It takes a reference, and if possible returns nushell/crates/nu-protocol/src/value/mod.rs Lines 1114 to 1142 in 2a2c1a3 During traversal
nushell/crates/nu-protocol/src/value/mod.rs Lines 1149 to 1159 in 2a2c1a3
|
|
Thanks |
Description
While working on something else, I noticed that
Value::follow_cell_pathreceivesself.While it would be ideal for the signature to be
(&'a self, cell_path) -> &'a Value, that's not possible because:Value::Customreturns newValues when indexed.So the signature becomes
(&'a self, cell_path) -> Cow<'a, Value>.Another complication that arises is, once a new
Valueis created, and it is further indexed, thecurrentvariable&'a Value, as the lifetime requirement means it can't refer to local variablesCow<'a, Value>, as once it becomes an owned value, it can't be borrowed ever again, ascurrentis derived from its previous value in further iterations. So once it's owned, it can't be indexed by reference, leading to more clonesWe need
currentto have two possible lifetimes'out: references derived from&self'local: references derived from an owned value stored in a local variableWith
current: MultiLife<'out, '_, Value>, we can traverse values with minimal clones, and we can transform it toCow<'out, Value>easily (MultiLife::Out -> Cow::Borrowed, MultiLife::Local -> Cow::Owned) to return itUser-Facing Changes
Tests + Formatting
After Submitting