forked from extern/nushell
# Description Despite the innocent-looking title, this PR involves quite a few backend changes as the existing LazyRecord trait was not at all friendly towards the idea of these values being generated on the fly from Nu code. In particular, here are a few changes involved: - The LazyRecord trait now involves a lifetime `'a`, and this lifetime is used in the return value of `get_column_names`. This means it no longer returns `'static str`s (but implementations still can return these). This is more stringent on the consumption side. - The LazyRecord trait now must be able to clone itself via a new `clone_value` method (as requiring `Clone` is not object safe). This pattern is borrowed from `Value::CustomValue`. - LazyRecord no longer requires being serde serializable and deserializable. These, in hand, allow for the following: - LazyRecord can now clone itself, which means that they don't have to be collected into a Record when being cloned. - This is especially useful in Stack, which is cloned on each repl line and in a few other cases. This would mean that _every_ LazyRecord instance stored in a variable would be collected in its entirety and cloned, which can be catastrophic for performance. See: `let nulol = $nu`. - LazyRecord's columns don't have to be static, they can have the same lifetime of the struct itself, so different instances of the same LazyRecord type can have different columns and values (like the new `NuLazyRecord`) - Serialization and deserialization are no longer meaningless, they are simply less. I would consider this PR very "drafty", but everything works. It probably requires some cleanup and testing, though, but I'd like some eyes and pointers first. # User-Facing Changes New command. New restrictions are largely internal. Maybe there are some plugins affected? Example of new command's usage: ``` lazy make --columns [a b c] --get-value { |name| print $"getting ($name)"; $name | str upcase } ``` You can also trivially implement something like `lazy make record` to take a record of closures and turn it into a getter-like lazy struct: ``` def "lazy make record" [ record: record ] { let columns = ($record | columns) lazy make --columns $columns --get-value { |col| do ($record | get $col) } } ``` Open to bikeshedding. `lazy make` is similar to `error make` which is also in the core commands. I didn't like `make lazy` since it sounded like some transformation was going on. # Tour for reviewers Take a look at LazyMake's examples. They have `None` as the results, as such they aren't _really_ correct and aren't being tested at all. I didn't do this because creating the Value::LazyRecord is a little tricky and didn't want to risk messing it up, especially as the necessary variables aren't available when creating the examples (like stack and engine state). Also take a look at NuLazyRecord's get_value implementation, or in general. It uses an Arc<Mutex<_>> for the stack, which must be accessed mutably for eval_block but get_value only provides us with a `&self`. This is a sad state of affairs, but I don't know if there's a better way. On the same code path, we also have pipeline handling, and any pipeline that isn't a Pipeline::Value will return Value::nothing. I believe returning a Value::Error is probably better, or maybe some other handling. Couldn't decide on which ShellError to settle with for that branch. The "unfortunate casualty" in the columns.rs file. I'm not sure just how bad that is, though, I simply had to fight a little with the borrow checker. A few leftover comments like derives, comments about the now non-existing serde requirements, and impls. I'll definitely get around to those eventually but they're in atm Should NuLazyRecord implement caching? I'm leaning heavily towards **yes**, this was one of the main reasons not to use a record of closures (besides convenience), but maybe it could be opt-out. I'd wonder about its implementation too, but a simple way would be to move a HashMap into the mutex state and keep cached values there.
91 lines
1.8 KiB
Rust
91 lines
1.8 KiB
Rust
mod alias;
|
|
mod break_;
|
|
mod collect;
|
|
mod const_;
|
|
mod continue_;
|
|
mod def;
|
|
mod def_env;
|
|
mod describe;
|
|
mod do_;
|
|
mod echo;
|
|
mod error_make;
|
|
mod export;
|
|
mod export_alias;
|
|
mod export_def;
|
|
mod export_def_env;
|
|
mod export_extern;
|
|
mod export_module;
|
|
mod export_use;
|
|
mod extern_;
|
|
mod for_;
|
|
pub mod help;
|
|
pub mod help_aliases;
|
|
pub mod help_commands;
|
|
pub mod help_externs;
|
|
pub mod help_modules;
|
|
mod help_operators;
|
|
mod hide;
|
|
mod hide_env;
|
|
mod if_;
|
|
mod ignore;
|
|
mod lazy_make;
|
|
mod let_;
|
|
mod loop_;
|
|
mod match_;
|
|
mod module;
|
|
mod mut_;
|
|
pub(crate) mod overlay;
|
|
mod return_;
|
|
mod try_;
|
|
mod use_;
|
|
mod version;
|
|
mod while_;
|
|
|
|
pub use alias::Alias;
|
|
pub use break_::Break;
|
|
pub use collect::Collect;
|
|
pub use const_::Const;
|
|
pub use continue_::Continue;
|
|
pub use def::Def;
|
|
pub use def_env::DefEnv;
|
|
pub use describe::Describe;
|
|
pub use do_::Do;
|
|
pub use echo::Echo;
|
|
pub use error_make::ErrorMake;
|
|
pub use export::ExportCommand;
|
|
pub use export_alias::ExportAlias;
|
|
pub use export_def::ExportDef;
|
|
pub use export_def_env::ExportDefEnv;
|
|
pub use export_extern::ExportExtern;
|
|
pub use export_module::ExportModule;
|
|
pub use export_use::ExportUse;
|
|
pub use extern_::Extern;
|
|
pub use for_::For;
|
|
pub use help::Help;
|
|
pub use help_aliases::HelpAliases;
|
|
pub use help_commands::HelpCommands;
|
|
pub use help_externs::HelpExterns;
|
|
pub use help_modules::HelpModules;
|
|
pub use help_operators::HelpOperators;
|
|
pub use hide::Hide;
|
|
pub use hide_env::HideEnv;
|
|
pub use if_::If;
|
|
pub use ignore::Ignore;
|
|
pub use lazy_make::LazyMake;
|
|
pub use let_::Let;
|
|
pub use loop_::Loop;
|
|
pub use match_::Match;
|
|
pub use module::Module;
|
|
pub use mut_::Mut;
|
|
pub use overlay::*;
|
|
pub use return_::Return;
|
|
pub use try_::Try;
|
|
pub use use_::Use;
|
|
pub use version::Version;
|
|
pub use while_::While;
|
|
//#[cfg(feature = "plugin")]
|
|
mod register;
|
|
|
|
//#[cfg(feature = "plugin")]
|
|
pub use register::Register;
|