nushell/crates/nu_plugin_custom_values/src/second_custom_value.rs
Devyn Cairns cbf7feef22
Make drop notification timing for plugin custom values more consistent (#12341)
# Description
This keeps plugin custom values that have requested drop notification
around during the lifetime of a plugin call / stream by sending them to
a channel that gets persisted during the lifetime of the call.

Before this change, it was very likely that the drop notification would
be sent before the plugin ever had a chance to handle the value it
received.

Tests have been added to make sure this works - see the `custom_values`
plugin.

cc @ayax79 

# User-Facing Changes
This is basically just a bugfix, just a slightly big one.

However, I did add an `as_mut_any()` function for custom values, to
avoid having to clone them. This is a breaking change.
2024-04-04 09:13:25 +02:00

82 lines
2.2 KiB
Rust

use std::cmp::Ordering;
use nu_protocol::{CustomValue, ShellError, Span, Value};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct SecondCustomValue {
pub(crate) something: String,
}
impl SecondCustomValue {
pub fn new(content: &str) -> Self {
Self {
something: content.to_owned(),
}
}
pub fn into_value(self, span: Span) -> Value {
Value::custom(Box::new(self), span)
}
pub fn try_from_value(value: &Value) -> Result<Self, ShellError> {
let span = value.span();
match value {
Value::Custom { val, .. } => match val.as_any().downcast_ref::<Self>() {
Some(value) => Ok(value.clone()),
None => Err(ShellError::CantConvert {
to_type: "cool".into(),
from_type: "non-cool".into(),
span,
help: None,
}),
},
x => Err(ShellError::CantConvert {
to_type: "cool".into(),
from_type: x.get_type().to_string(),
span,
help: None,
}),
}
}
}
#[typetag::serde]
impl CustomValue for SecondCustomValue {
fn clone_value(&self, span: nu_protocol::Span) -> Value {
Value::custom(Box::new(self.clone()), span)
}
fn type_name(&self) -> String {
self.typetag_name().to_string()
}
fn to_base_value(&self, span: nu_protocol::Span) -> Result<Value, ShellError> {
Ok(Value::string(
format!(
"I used to be a DIFFERENT custom value! ({})",
self.something
),
span,
))
}
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
if let Value::Custom { val, .. } = other {
val.as_any()
.downcast_ref()
.and_then(|other: &SecondCustomValue| PartialOrd::partial_cmp(self, other))
} else {
None
}
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
}