Comments (13)
I have a proposition to possibly fix this, the problem however is that it would require some additional wrapping to get it to work. So the current problem with closures seems to be that they are unable to live long enough to actually be called whenever a signal gets triggered. This happens because they get passed to the C interface, but the Rust binding keeps owning them. Eventually, they will get cleaned up from the stack, after the function where they were created at returns. Then, when Glib and GTK+ actually need them, they end up invoking a closure with garbage data.
To fix this, we would have to claim ownership of the closures within the binding, and store them somewhere safe. What I am basically proposing is to keep track of signal mappings for GObjects
in a HashMap
or something similar, and add a dispatch
method to actually invoke the associated callbacks. This would look something like this example:
use std::collections::HashMap;
struct Signals {
signals: HashMap<String, Box<FnMut() -> ()>>
}
impl Signals {
fn new() -> Signals {
Signals { signals: HashMap::new() }
}
/* It may be possible to do this with lifetimes as well. */
fn connect(&mut self, name: String, callback: Box<FnMut() -> ()>) {
self.signals.insert(name, callback);
}
fn dispatch(&mut self, name: &String) {
let callback = match self.signals.get_mut(name) {
Some(callback) => callback,
_ => return
};
callback();
}
}
fn do_connect(signals: &mut Signals) {
let index = 42;
let name = "foo".to_string();
signals.connect(name, Box::new(move || println!("{}", index)));
}
fn main() {
let mut signals = Signals::new();
do_connect(&mut signals);
let name = "foo".to_string();
signals.dispatch(&name);
}
Then once this is in place, we can implement callbacks for the actual signals that will in turn use this signal map to dispatch the signals to their respective callbacks. So yes, the unfortunate part, is that we are essentially implementing our own signal system on top of an existing one. To further clarify what I mean: the low-level callbacks would simply be very generic ones that take all the arguments, and dispatch them into our signal system, and that we can either enable (by connecting them) or disable (by disconnecting them).
However, if we do it properly, we only have to connect the signals that have to be, i.e. if the a signal is not in use, it should not be in use in the low-level layer either.
Then there should be one final issue, and that is getting the callbacks to be generic enough that they can be used for the different events. This should be doable with either enums or traits, but this idea still requires some more polishing.
from gtk.
Owning is not a big issue, we can even let the lib own them like in this example: https://gist.github.com/gkoz/52ae8de5370ba99d9795
BTW I believe the handlers can't generally be FnMut
because they need to be reentrant.
Supporting different numbers and sets of arguments and converting them between foreign and native types is tough.
from gtk.
Maybe that signal-specific glue will just have to be done by hand similarly to the method calls.
from gtk.
I think they can be either Fn
or FnMut
, depending on whether you want the changes to the environment to be persistent or not (in the sense that if you don't want to use the environment of a previous call, then FnMut
is useless).
from gtk.
I've been looking at your example, and I see how you could have the library own the closure with proper deletion by passing a callback to clean it up. I have to admit, that I still had to read about what transmute
actually does, and now that I understand that, I also understand why your code works :).
Ownership was one of my concerns because the original code in upstream takes a mutable reference to a closure, instead of actually moving the closure to the function, but that issue is solved with what you are currently doing.
I think it makes sense to implement the signals by hand for the moment being, I am willing to help with that, and then later on, attempt to generalise it. Especially, since we can then observe the various cases that have to be supported.
from gtk.
@StephanvanSchaik: That'd be very nice ! Any help is appreciated ^^
from gtk.
I think they can be either Fn or FnMut, depending on whether you want the changes to the environment to be persistent or not.
It's conceivable that a signal handler can end up causing the same signal to be processed in a recursive manner (example). I believe such use isn't compatible with FnMut
, which expects its mutable borrows to be unique therefore isn't reentrant.
from gtk.
As there is some momentum for fixing the closures, I'll try to integrate the example from #16 (comment) into the tree without breaking existing signals support.
from gtk.
There's actually an even stricter constraint to what we can give to the lib. It's Fn + 'static
i.e. the closure wouldn't be allowed to capture any non-static references at all (we still can move values into it though).
The approach of owning the closures ourselves would be more flexible but it's not clear it'd gain us anything in practice. It could probably be done incrementally afterwards. One more practical problem with that is that rc::Weak
isn't going to be stable in Rust 1.0.
One more alternative is letting each object (e.g. a button) own its signal handlers storage and disconnect them on destruction. That would probably need rc::Weak
too.
from gtk.
The proposed solution for review: #33
from gtk.
The initial implementation has been merged, we need to migrate all of the signals into this module now and then remove the old infrastructure.
from gtk.
cc @oakes
from gtk.
So the closures aren't broken any more. They're just not very convenient now... :)
from gtk.
Related Issues (20)
- constants GDK_BUTTON_* missing? HOT 3
- are we sure destroy() is never needed? HOT 9
- What is the best way to make integration tests with GTK? HOT 4
- Include libportal in gtk-rs? HOT 4
- Null pointer panic on `GestureZoom::connect_end` HOT 4
- Crash when touching entry icon HOT 8
- Cannot add new tabs in GtkNotebook HOT 3
- glib and gio compatibility scheme HOT 4
- FileChooserNative memory management issue HOT 4
- Broken requirements URL in README.md HOT 6
- Broken README link
- gtk::PopoverMenu::popup doesn't work for PopoverMenu HOT 4
- Problems with `gtk::Image` update through `Pixbuf` HOT 1
- Using from another package fails on Windows with GLib-GIO-CRITICAL ** This application can not open files. HOT 2
- Pressing the mouse button yields two button events HOT 4
- Building gtk-rs runs out of memory on 2GB device, which I think means compiling any basic graphical app won't work there HOT 18
- GestureDrag does not signal events HOT 1
- Support GTK 4 HOT 2
- Add GtkImageMenuItem support HOT 1
- GLArea and OpenGL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gtk.