Closed gsingh93 closed 9 years ago
It can be for multiple reasons : one of the needed types isn't binded yet, the code hasn't been updated or it is just a forgotten one. Don't hesitate to bind it if you want or need it !
I'll look into it. I haven't messed with FFI before, but I'll see what I can do.
On Sat, Jan 3, 2015, 3:41 PM Guillaume Gomez notifications@github.com wrote:
It can be for multiple reasons : one of the needed types isn't binded yet, the code hasn't been updated or it is just a forgotten one. Don't hesitate to bind it if you want or need it !
— Reply to this email directly or view it on GitHub https://github.com/jeremyletang/rgtk/issues/173#issuecomment-68608758.
Don't hesitate to ask if you need any help.
Ok, I'm starting to figure out how this works. Uncommenting the methods I need causes no problems, so the only issue is implementing the safe wrappers. I need to implement GtkBuilder which inherits from GObject. Where would I put that, and is there an example of another object I can look at?
Also, the generated bindings seem to refer to C_GObject
, but I had to change it to glib::ffi::C_GObject
. I see other references to glib::ffi::C_GObject
in the code. Can I just import this at the top and just refer to C_GObject
, or is there a reason it was done like this (i.e. some kind of import shadowing issue)?
You have an example here. You need to create a C_GtkBuild object in ffi if it doesn't exist and call the impl_GObjectFunctions!
macro to implement the needed functions.
Normally, you'll never need to have to handle C_GObject yourself, but maybe I'm missing something. Do you have an example where you'd need to handle C_GObject yourself ?
Regarding C_GObject
, here's an example of other code that's referencing it, and here's an example of a method I need that's returning it.
For the gtk cast, it's very common but I'm not sure it's used. For this function, you can do something like :
pub fn get_object<T: GObject>(&self) -> T {
...
}
But I don't remember if we created a g_object trait. I check that !
I created a file called builder.rs
in src/gtk/widgets
, but it's not getting built. Not only can code not import it, if I put invalid Rust code in it, I get no build errors. Do I have to do something other than add the file to get it to build?
You have to add :
mod builder.rs;
in src/gtk/widgets/mod.rs
and also add a pub use and all the stuff. You'll have to do it in the src/gtk/mod.rs
file. Look at how we did with other files.
Now there is a GObjectTrait so you will be able to write the code like I did just above.
I'm not sure what type to return:
pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
let tmp = unsafe {
name.with_c_str(|c_str| {
ffi::gtk_builder_get_object(self.pointer, c_str)
})
};
// TODO: Return object
}
I don't think it'll work but it's worth a try:
pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
let tmp = unsafe {
name.with_c_str(|c_str| {
ffi::gtk_builder_get_object(self.pointer, c_str)
})
};
unsafe {
::std::mem::transmute(GtkBuild {
pointer: ::std::mem::transmute(tmp)
})
}
}
Yes, it's SUPER ugly.
error: cannot transmute to or from a type that contains type parameters in its interior
Should there be some GObject struct that we should be able to return here?
Hum... Try this:
pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
let tmp = unsafe {
name.with_c_str(|c_str| {
ffi::gtk_builder_get_object(self.pointer, c_str)
})
};
unsafe {
::std::mem::transmute(GtkBuild {
pointer: tmp
})
}
}
mismatched types: expected `*mut gtk::ffi::C_GtkBuilder`, found `*mut glib::ffi::C_GObject` (expected struct gtk::ffi::C_GtkBuilder, found struct glib::ffi::C_GObject)
If you want to take a closer look, I've pushed what I have so far here: https://github.com/gsingh93/rgtk
Oh my bad, I forgot:
pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
let tmp = unsafe {
name.with_c_str(|c_str| {
ffi::gtk_builder_get_object(self.pointer, c_str)
})
};
unsafe {
::std::mem::transmute(GtkBuild {
pointer: tmp as *mut ffi::C_GtkBuilder
})
}
}
My goal in here is just to have a similar struct to the one which will be returned. They're pretty much all the same.
Back to the old error:
src/gtk/widgets/builder.rs:91:13: 91:34 error: cannot transmute to or from a type that contains type parameters in its interior [E0139]
src/gtk/widgets/builder.rs:91 ::std::mem::transmute(Builder {
^~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Ok, I'll watch that later. Comment this part of the code and continue.
The other methods I needed are working, so I just need to get this one working. Let me know when you get around to looking at it.
I'd liked to but I can't work on it as long as this PR isn't fixed and merged.
Can you look at this again?
I'm taking a look as soon as possible (today or tomorrow).
@gsingh93: Look at the PR #188.
Why are methods like these commented out in the source:
This particular method is a very useful one, as it's used to instantiate the view from a Glade file.