2024-03-23 19:29:54 +01:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
|
|
|
use nu_plugin::{EngineInterface, EvaluatedCall, Plugin, SimplePluginCommand};
|
|
|
|
use nu_plugin_test_support::PluginTest;
|
|
|
|
use nu_protocol::{
|
2024-03-27 11:59:57 +01:00
|
|
|
CustomValue, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value,
|
2024-03-23 19:29:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq)]
|
|
|
|
struct CustomU32(u32);
|
|
|
|
|
|
|
|
impl CustomU32 {
|
|
|
|
pub fn into_value(self, span: Span) -> Value {
|
Rename `Value::CustomValue` to `Value::Custom` (#12309)
# Description
The second `Value` is redundant and will consume five extra bytes on
each transmission of a custom value to/from a plugin.
# User-Facing Changes
This is a breaking change to the plugin protocol.
The [example in the protocol
reference](https://www.nushell.sh/contributor-book/plugin_protocol_reference.html#value)
becomes
```json
{
"Custom": {
"val": {
"type": "PluginCustomValue",
"name": "database",
"data": [36, 190, 127, 40, 12, 3, 46, 83],
"notify_on_drop": true
},
"span": {
"start": 320,
"end": 340
}
}
}
```
instead of
```json
{
"CustomValue": {
...
}
}
```
# After Submitting
Update plugin protocol reference
2024-03-27 22:10:56 +01:00
|
|
|
Value::custom(Box::new(self), span)
|
2024-03-23 19:29:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[typetag::serde]
|
|
|
|
impl CustomValue for CustomU32 {
|
|
|
|
fn clone_value(&self, span: Span) -> Value {
|
|
|
|
self.clone().into_value(span)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_name(&self) -> String {
|
|
|
|
"CustomU32".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_base_value(&self, span: Span) -> Result<Value, ShellError> {
|
|
|
|
Ok(Value::int(self.0 as i64, span))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
|
|
self
|
2024-04-04 09:13:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
|
|
|
|
self
|
2024-03-23 19:29:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
|
|
|
|
other
|
|
|
|
.as_custom_value()
|
|
|
|
.ok()
|
|
|
|
.and_then(|cv| cv.as_any().downcast_ref::<CustomU32>())
|
|
|
|
.and_then(|other_u32| PartialOrd::partial_cmp(self, other_u32))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CustomU32Plugin;
|
|
|
|
struct IntoU32;
|
|
|
|
struct IntoIntFromU32;
|
|
|
|
|
|
|
|
impl Plugin for CustomU32Plugin {
|
|
|
|
fn commands(&self) -> Vec<Box<dyn nu_plugin::PluginCommand<Plugin = Self>>> {
|
|
|
|
vec![Box::new(IntoU32), Box::new(IntoIntFromU32)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SimplePluginCommand for IntoU32 {
|
|
|
|
type Plugin = CustomU32Plugin;
|
|
|
|
|
2024-03-27 11:59:57 +01:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"into u32"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Convert a number to a 32-bit unsigned integer"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name()).input_output_type(Type::Int, Type::Custom("CustomU32".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
example: "340 | into u32",
|
|
|
|
description: "Make a u32",
|
|
|
|
result: Some(CustomU32(340).into_value(Span::test_data())),
|
|
|
|
}]
|
2024-03-23 19:29:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_plugin: &Self::Plugin,
|
|
|
|
_engine: &EngineInterface,
|
|
|
|
call: &EvaluatedCall,
|
|
|
|
input: &Value,
|
|
|
|
) -> Result<Value, LabeledError> {
|
|
|
|
let value: i64 = input.as_int()?;
|
|
|
|
let value_u32 = u32::try_from(value).map_err(|err| {
|
|
|
|
LabeledError::new(format!("Not a valid u32: {value}"))
|
|
|
|
.with_label(err.to_string(), input.span())
|
|
|
|
})?;
|
|
|
|
Ok(CustomU32(value_u32).into_value(call.head))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SimplePluginCommand for IntoIntFromU32 {
|
|
|
|
type Plugin = CustomU32Plugin;
|
|
|
|
|
2024-03-27 11:59:57 +01:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"into int from u32"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Turn a u32 back into a number"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name()).input_output_type(Type::Custom("CustomU32".into()), Type::Int)
|
2024-03-23 19:29:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_plugin: &Self::Plugin,
|
|
|
|
_engine: &EngineInterface,
|
|
|
|
call: &EvaluatedCall,
|
|
|
|
input: &Value,
|
|
|
|
) -> Result<Value, LabeledError> {
|
|
|
|
let value: &CustomU32 = input
|
|
|
|
.as_custom_value()?
|
|
|
|
.as_any()
|
|
|
|
.downcast_ref()
|
|
|
|
.ok_or_else(|| ShellError::TypeMismatch {
|
|
|
|
err_message: "expected CustomU32".into(),
|
|
|
|
span: input.span(),
|
|
|
|
})?;
|
|
|
|
Ok(Value::int(value.0 as i64, call.head))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_u32_examples() -> Result<(), ShellError> {
|
|
|
|
PluginTest::new("custom_u32", CustomU32Plugin.into())?.test_command_examples(&IntoU32)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_int_from_u32() -> Result<(), ShellError> {
|
|
|
|
let result = PluginTest::new("custom_u32", CustomU32Plugin.into())?
|
|
|
|
.eval_with(
|
|
|
|
"into int from u32",
|
|
|
|
PipelineData::Value(CustomU32(42).into_value(Span::test_data()), None),
|
|
|
|
)?
|
Replace `ExternalStream` with new `ByteStream` type (#12774)
# Description
This PR introduces a `ByteStream` type which is a `Read`-able stream of
bytes. Internally, it has an enum over three different byte stream
sources:
```rust
pub enum ByteStreamSource {
Read(Box<dyn Read + Send + 'static>),
File(File),
Child(ChildProcess),
}
```
This is in comparison to the current `RawStream` type, which is an
`Iterator<Item = Vec<u8>>` and has to allocate for each read chunk.
Currently, `PipelineData::ExternalStream` serves a weird dual role where
it is either external command output or a wrapper around `RawStream`.
`ByteStream` makes this distinction more clear (via `ByteStreamSource`)
and replaces `PipelineData::ExternalStream` in this PR:
```rust
pub enum PipelineData {
Empty,
Value(Value, Option<PipelineMetadata>),
ListStream(ListStream, Option<PipelineMetadata>),
ByteStream(ByteStream, Option<PipelineMetadata>),
}
```
The PR is relatively large, but a decent amount of it is just repetitive
changes.
This PR fixes #7017, fixes #10763, and fixes #12369.
This PR also improves performance when piping external commands. Nushell
should, in most cases, have competitive pipeline throughput compared to,
e.g., bash.
| Command | Before (MB/s) | After (MB/s) | Bash (MB/s) |
| -------------------------------------------------- | -------------:|
------------:| -----------:|
| `throughput \| rg 'x'` | 3059 | 3744 | 3739 |
| `throughput \| nu --testbin relay o> /dev/null` | 3508 | 8087 | 8136 |
# User-Facing Changes
- This is a breaking change for the plugin communication protocol,
because the `ExternalStreamInfo` was replaced with `ByteStreamInfo`.
Plugins now only have to deal with a single input stream, as opposed to
the previous three streams: stdout, stderr, and exit code.
- The output of `describe` has been changed for external/byte streams.
- Temporary breaking change: `bytes starts-with` no longer works with
byte streams. This is to keep the PR smaller, and `bytes ends-with`
already does not work on byte streams.
- If a process core dumped, then instead of having a `Value::Error` in
the `exit_code` column of the output returned from `complete`, it now is
a `Value::Int` with the negation of the signal number.
# After Submitting
- Update docs and book as necessary
- Release notes (e.g., plugin protocol changes)
- Adapt/convert commands to work with byte streams (high priority is
`str length`, `bytes starts-with`, and maybe `bytes ends-with`).
- Refactor the `tee` code, Devyn has already done some work on this.
---------
Co-authored-by: Devyn Cairns <devyn.cairns@gmail.com>
2024-05-16 16:11:18 +02:00
|
|
|
.into_value(Span::test_data())?;
|
2024-03-23 19:29:54 +01:00
|
|
|
assert_eq!(Value::test_int(42), result);
|
|
|
|
Ok(())
|
|
|
|
}
|