Git Product home page Git Product logo

Comments (4)

Timidger avatar Timidger commented on August 19, 2024

Could this be solved by borrowing the value (which requires knowing its type, which shouldn't be a big hurdle for us) and then getting a pointer to the inner value?

e.g:

let ref = user_data.borrow::<Tag>()?;
println!("Pointer is {:p}", &*ref as *const Tag);

from rlua.

psychon avatar psychon commented on August 19, 2024

Uhm... now I wonder what the problem was that made me conclude that the above does not work. However, I have no idea what that was....O.o
(Perhaps I got confused by the get_object() function in way-cooler since that clones the object...)

Sorry for the noise!

from rlua.

kyren avatar kyren commented on August 19, 2024

I'm glad you got it figured out! I didn't know about __name before, and apparently it's used by some error reporting functions as well? I might look into allowing that to be set on userdata metatables, but like everything else on userdata metatables it would probably be the same for all instances of a UserData.

from rlua.

psychon avatar psychon commented on August 19, 2024

Could this be solved by borrowing the value (which requires knowing its type, which shouldn't be a big hurdle for us) and then getting a pointer to the inner value?

The short version: Yes, but I forgot to do a dereference (*) when testing. You apparently did not forget this. :-)


Okay, I finally had the time to actually try this and... it does not work.

extern crate rlua;

use rlua::{Lua, AnyUserData, UserData, Result};

struct MyData();

impl UserData for MyData {}

fn run() -> Result<()> {
    let lua = Lua::new();
    lua.globals().set("object", MyData())?;

    let value = lua.globals().get::<_, AnyUserData>("object")?;
    println!("{:?}", &value.borrow::<MyData>()? as *const _);
    println!("{:?}", &value.borrow::<MyData>()? as *const _);
    println!("{:?}", &value.borrow::<MyData>()? as *const _);

    Ok(())
}

fn main() {
    run().unwrap()
}

Output (example):

0x7ffe4c9944d0
0x7ffe4c9946a0
0x7ffe4c994870

So apparently I already tried this, but forgot about the details when I closed this issue five days ago.

The return type of borrow() is std::cell::Ref, so what the code above actually prints is the address of this instance of Ref.
Instead, I need to do the following (note the extra *) so that Ref is dereferenced and I actually take the address of the contained object:

    println!("{:?}", &value.borrow::<MyData>()? as *const _);

...and with this modification, things actually start working.

from rlua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.