-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wrapper for SDL_AddEventWatch and SDL_DelEventWatch #1070
Conversation
00f8cda
to
911fea4
Compare
Lifetimes seem correct, you added However I don't really know what you mean by "the function may be called in multiple threads". It all depends of when the callback is called exactly, which is probably explained somewhere in SDL2's internal code (the doc is not clear enough on this topic). If the acllback is called as soon as the event is triggered, then it's probably the latter, however if it's called right about "event_pump" processing is done, then it's probably single-threaded and the former implementation.
Yes, it's standard Rust to do it like so. |
Thank you for your reply! As for concurrency, I asked the community forum for the detail. As long as there's a feedback, I'll adjust is as needed. Is there some other things to do before it can be merged? |
The callback is indeed run on push, but apparently there is a mutex for the watchers in PushEvent, so that the callbacks are never run at the same time: https://github.com/libsdl-org/SDL/blob/main/src/events/SDL_events.c#L805 So your code seems correct. Tell me if you have anything else to add, otherwise I can merge this. |
@Cobrand Thanks for your reading the code! I realized that closures are actually difficult to describe its type, so I'm planning to extract it to a trait. As long as it's done it's ok to merge. |
@Cobrand Done! |
Mmh I'm not sure it's the right call to create a trait just for that, is there a reason to not use FnMut like you did before? |
@Cobrand Several reasons.
fn create_event_watch() -> EventWatch<impl FnMut(Event) -> ()> {
// generate
} or more trickier (if you don't want to manipulate the event watch at all): fn create_event_watch() -> impl Drop {
// generate
} The former is lengthy and the latter restricts the usability. If you want to store it into a struct, things get worse.
Of course I loved the handy closure approach, but while I'm using I realized the problem mentioned above, and that's why I switched to trait. |
Could you try to at least |
@Cobrand That's a good idea, I didn't came up with that! I added. |
Thanks! Let's merge this. |
This should include my PR Rust-SDL2/rust-sdl2#1070 .
Add wrapper for SDL_AddEventWatch and SDL_DelEventWatch
Closes #1023.
The new function enables to use an event watcher in a safe, user-friendly way. We can call
EventSubsystem::add_event_watch
with a closure parameter as a callback to listen to the events. The callback is automatically disabled when dropped.When I wrote the code I largely referenced
AudioDevice::open
,AudioSpecDesired::convert_to_ll
andaudio_callback_marshall
insrc/audio.rs
. Great thanks to them.Concerns:
Send
orSync
as additional bounds? (I'm not quite proficient at unsafe Rust, sorry)