Skip to content

Having trouble with LuaFunction::wrap_raw() in v0.10 #474

@alerque

Description

@alerque

I'm playing around with RC.1 trying to figure out how to handle my traits and types ergonomically, currently using decasify as a place to experiment without the project being too big to easily fiddle with.

Note that if this issue doesn't make sense I can try packaging it into an MWE.

I'm trying to simply the full set of extra functions and type conversions I used in v0.9.x (see also #424) and have a local type (Chunk) that I have implemented Into for a number of common string types (&str, String, &Cow<'_, str>, etc.). I also have functions that accept imp Into<Chunk>. What I'm trying to do is wrap those functions in a Lua module without writing a bunch of shim functions.

Here is a sample function signature I'm using for testing. For ease of testing I'm just discarding the data for now and just trying to get the types right.

pub type Result<T> = anyhow::Result<T>;

#[mlua::lua_module]
fn decasify(lua: &Lua) -> LuaResult<LuaTable> {
    let exports = lua.create_table().unwrap();
    exports.set("test", LuaFunction::wrap_raw(test_func))?;
    Ok(exports)
}

pub fn test_func(_s: String) -> Result<String> {
    Ok("some pseudo return data".into())
}

This works by itself so the basic bones of my test seem to be in place.

Now lets look at my trait. By itself the Into<Chunk> compiles fine, but I can't seem to use it in a function:

impl FromLua for Chunk {
    fn from_lua(_value: LuaValue, _: &Lua) -> LuaResult<Self> {
        Ok(Self {})
    }
}

pub fn test_func(_chunk: impl Into<Chunk>, _s: String) -> Result<String> {
    Ok("some pseudo return data".into())
}

This falls apart because it can't figure out what to do with the function signature:

 1  error[E0283]: type annotations needed
   --> src/lua.rs:27:26
    |
 27 |     exports.set("test", LuaFunction::wrap_raw(test_func))?;
    |                          ^^^^^^^^^^^^^^^^^^^^^ ----- type must be known at this point
    |                          |
    |                          cannot infer type of the type parameter `F` declared on the associated function `wrap_raw`
    |
    = note: cannot satisfy `_: Into<content::Chunk>`
 note: required by a bound in `test_func`
   --> src/lib.rs:52:27
    |
 52 | pub fn test_func(_chunk: impl Into<Chunk>, _s: String) -> Result<String> {
    |                           ^^^^^^^^^^^ required by this bound in `test_func`
 help: consider specifying the generic arguments
    |
 27 |     exports.set("test", LuaFunction::wrap_raw::<_, (A, std::string::String)>(test_func))?;
    |                                               +++++++++++++++++++++++++++++++

I can tinker with that wrapper a bit and get it to compile:

exports.set("test", LuaFunction::wrap_raw::<_, (String, String)>(test_func))?;

But that seems like an unnecessary hoop to jump through, and overly restricting. I don't actually want the Lua function to only accept strings, I want to be able to implement this for other possible Lua types as well.

Is it apparent what I'm missing or is this a bug or a missing feature? Or does none of this make sense and I should work up an actual stand alone MWE?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions