rune-rs / rune

An embeddable dynamic programming language for Rust.
https://rune-rs.github.io
Apache License 2.0
1.7k stars 85 forks source link

`#[rune::function]` fails when adding a 6th argument #755

Open VorpalBlade opened 1 month ago

VorpalBlade commented 1 month ago

This compiles:

    #[rune::function]
    fn add_from_sysusers(
        &mut self,
        package_manager: &PackageManager,
        //package: &str,
        config_file: &str,
        user_ids: Vec<(String, u32)>,
        group_ids: Vec<(String, u32)>,
    ) -> anyhow::Result<()> {
        //let sysusers = String::from_utf8(package_manager.file_contents(package, config_file)?)?;
        todo!()
    }

Uncommenting the package parameter gives the following (cryptic) error though:

error[E0277]: the trait bound `for<'a, 'b, 'c, 'd> fn(&'a mut Passwd, &'b PackageManager, &'c str, &'d str, std::vec::Vec<(std::string::String, u32)>, std::vec::Vec<(std::string::String, u32)>) -> std::result::Result<(), anyhow::Error> {Passwd::__rune_fn__add_from_sysusers}: InstanceFunction<_, _>` is not satisfied
   --> crates/konfigkoll_script/src/plugins/passwd.rs:150:5
    |
150 |     #[rune::function]
    |     ^^^^^^^^^^^^^^^^^ the trait `InstanceFunction<_, _>` is not implemented for fn item `fn(&mut Passwd, &PackageManager, &str, &str, Vec<(String, u32)>, Vec<(String, u32)>) -> ... {...::__rune_fn__add_from_sysusers}`
    |
note: required by a bound in `FunctionMetaKind::instance`
   --> /home/arvid/src/paketkoll/patches/rune/crates/rune/src/module/function_meta.rs:367:12
    |
364 |     pub fn instance<N, F, A, K>(name: N, f: F) -> alloc::Result<Self>
    |            -------- required by a bound in this associated function
...
367 |         F: InstanceFunction<A, K>,
    |            ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `FunctionMetaKind::instance`
    = note: this error originates in the attribute macro `rune::function` (in Nightly builds, run with -Z macro-backtrace for more info)

In fact, commenting out any of the 6 parameters make it work. I cannot find any mention of a 6 parameter limit in the docs?

VorpalBlade commented 1 month ago

In case you need to reproduce this: https://github.com/VorpalBlade/paketkoll/tree/feature/konfigkoll (commit 588104d5), in the file crates/konfigkoll_script/src/plugins/passwd.rs, the function add_from_sysusers.

Notes:

udoprog commented 1 month ago

It's not explicitly spelled out in the documentation, but the Function trait is only implemented up to five parameters.

It's a bit of a pain adding more, since they have to be a permutation between three different traits.

The background for why this is needed is here. Previously the references used were unbound and this pattern is currently needed to unbind them.

However, if we can live with only one trait being implemented for the auxiliary parameters (like FromValue), that might work too!

VorpalBlade commented 1 month ago

Hm, confusingly when I looked at InstanceFunction it was implemented for way more parameters.

Since you already use a macro (declarative) to generate these impls, what is the cost of repeating it for a few more parameters? Is it build time due to combinatorial explosion?

In that case only supporting Value for parameter 7 up to N makes sense as a compromise.

udoprog commented 1 month ago

If you want to play around with the problem space, here's a playground I put together.

This is another playground showing how we currently solve it with references.

udoprog commented 1 month ago

Since you already use a macro (declarative) to generate these impls, what is the cost of repeating it for a few more parameters? Is it build time due to combinatorial explosion?

I don't remember. Please try it out if you have the time!