rune
rune copied to clipboard
Support `derive(Any)` type protocols
What I want is some way to use e.g. derived functions or trait implementations in general with derive(Any).
#[derive(Any, Debug, Display)]
#[display(fmt = "name = {value}")]
// STRING_DISPLAY uses the `Display` trait implementation
#[rune(item = ::std::env, protocols(STRING_DISPLAY, DEBUG = Self::debug)]
struct Var {
name: String,
}
impl Var {
// This would map debug to display instead of using the rust debug.
fn debug(&self, buf: &mut String) -> fmt::Result {
write!(buf, "{self}");
}
}
I have put together something here: https://github.com/rune-rs/rune/commit/bbb389798dc9a5618c4bb596bb4e501865b52837 though I think it could have a nicer interface:
#[derive(Any, Clone, Display)]
#[rune(item = ::std::env, protocols = [STRING_DISPLAY])]
#[display(fmt = "{}", "value.as_deref().unwrap_or_default()")]
struct Var {
name: String,
value: Option<String>,
}
I would be willing to implement this.