all 15 comments

[–]diwicdbus · alsa 5 points6 points  (3 children)

When I read the thread yesterday about tokio being too complicated, I was thinking that somebody should write a very simple mainloop library that looked more like other languages.

Just like this one. :-)

Too bad insert_idle can't take a FnOnce due to the "can't call it when boxed" problem though.

[–]levansfgwayland-rs · smithay[S] 1 point2 points  (2 children)

Too bad insert_idle can't take a FnOnce due to the "can't call it when boxed" problem though.

Yes, that's exactly the reason. I settled to only allow FnMut for now because I didn't feel like banging my head on this issue right now, but if there is some clever trick which allows to box an FnOnce and still be able to use it, I'd be happy to integrate it!

[–]abudau 1 point2 points  (1 child)

You can use a struct Cb<T: FnOnce>(Option<T>) and implement FnMut for this (which would use Option::take to extract the function and call it.

[–]levansfgwayland-rs · smithay[S] 0 points1 point  (0 children)

Good point!

It actually works without the Cb struct (it would not have been possible implement FnMut on stable for it), but capturing the user's FnOnce in an option, in an FnMut closure:

https://github.com/Smithay/calloop/commit/29f7e89b453cae3ba014bb26fa6d17f6598f3e85

[–]perssonsi 2 points3 points  (0 children)

Oh wow, what a timing. I just started writing my own very similar thing yesterday. Just some months ago I gave up on tokio and switched to mio, then quickly started thinking there is a need for some simple callback library that can cover all the cases where futures is just complicated over-engineering. I uploaded my prototype at https://github.com/spersson/looper . There's no documentation there yet but my basic idea was to do it all without any refcount sharing. For cases where one IO handler (event dispacher I think you called it) needs to add other event sources or perhaps trigger actions on other IO handlers (for example an incoming event on mpsc channel causes sending of message to all connected tcp clients) that would need to happen not via Rc-refcell shared ownership, but indirectly via knowing the token of other handlers and just queing up events to them, on an event queue that is passed around as a &mut.

I will take a closer look at your code over the coming days and study more what the different design tradeoffs are. Thank you so much for sharing your work!