Skip to content

Implementing C helper functions for Rust UserData #545

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

You must be logged in to vote

Check my example, it works:

struct Vec2(f32, f32);

let lua = Lua::new();

let push_vector = lua.create_function(|lua, (x, y): (f32, f32)| {
    let vec = Vec2(x, y);
    lua.create_any_userdata(vec)
})?;
lua.set_named_registry_value("push_vector", push_vector)?;

let get_vector = lua.create_function(|_, mut ud: UserDataRefMut<Vec2>| {
    let vec: *mut Vec2 = &mut *ud;
    Ok(Value::LightUserData(LightUserData(vec as *mut c_void)))
})?;
lua.set_named_registry_value("get_vector", get_vector)?;

unsafe extern "C" fn lua_isvector(L: *mut lua_State, idx: c_int) -> c_int {
    !lua_tovector(L, idx).is_null() as c_int
}

unsafe extern "C" fn lua_pushvector(L: *mut lua_State, x: f32, y: f32) {

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@bobbens
Comment options

@khvzak
Comment options

Answer selected by bobbens
@bobbens
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants