Skip to content

Mapping a Lua function over a Rust Iterator #56

@jrobsonchase

Description

@jrobsonchase

I suspect I'm running up against the downside of the "stackless" style here.

In Rust, I have a reference to some World that I'm wrapping in a piccolo_util::Frozen and sticking in a global Lua variable to be accessed from Lua. This works great for World methods that return static things. When doing more complex queries on the world though, I end up with an iterator which contains a reference to the World, which thanks to Frozen, is only valid within the call to Frozen::with.

My first approach was to pass a Function from Lua to Rust which takes the iterator's Item, and create an Executor to run it inside the callback. Then I found this blurb in the Executor docs:

Executors are not meant to be used from callbacks at all, and Executors should not be nested. Instead, use the normal mechanisms for callbacks to call Lua code so that it is run on the same executor calling the callback.

I'm guessing these "normal mechanisms" are CallbackReturns like Call and Sequence? Since neither the World reference nor the iterator can escape the Frozen::with callback, it doesn't seem like I'd be able to wrap and return the iterator directly, and it can't be recreated and resumed from an arbitrary point. And even if that were possible, it would be handing out references which themselves couldn't be Function::bind'ed and escape.

It seems like the best approach available to me is to copy/collect the iteration results into a Table and return that back to Lua, but I'd like to avoid that overhead if possible.

Is there anything I've missed? Any other approaches to consider?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions