Skip to content

How to store functions in UserData? #30

Answered by khvzak
Absolucy asked this question in Q&A
Discussion options

You must be logged in to vote

Unfortunately you cannot refer to a LuaFunction in that way :(
A possible solution could be storing Vec<Route> inside Lua (in a UserData) and refer to it using get_user_value() / set_user_value() or use Registry (via named_registry_value).

An example using get/set_user_value

pub struct Router;

impl UserData for Router {
    fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_function(
            "register",
            |lua: &Lua, (router, _, callback): (AnyUserData, LuaValue, LuaFunction)| {
                // To get `router` as a Router instance:
                // let router = router.borrow::<Router>()?;
                let routes = match router.

Replies: 2 comments 11 replies

Comment options

You must be logged in to vote
1 reply
@Absolucy
Comment options

Comment options

You must be logged in to vote
10 replies
@Absolucy
Comment options

@khvzak
Comment options

@Absolucy
Comment options

@khvzak
Comment options

@Absolucy
Comment options

Answer selected by Absolucy
# for free to join this conversation on GitHub. Already have an account? # to comment
Category
Q&A
Labels
None yet
2 participants