rune icon indicating copy to clipboard operation
rune copied to clipboard

Support `derive(Any)` type protocols

Open ModProg opened this issue 2 years ago • 1 comments

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>,
}

ModProg avatar Jul 19 '23 13:07 ModProg

I would be willing to implement this.

ModProg avatar Jul 19 '23 13:07 ModProg