Open detly opened 2 years ago
That sounds like a solid proposal, thanks for putting all that on writing. I hadn't given a lot of thought about this, and what you suggest makes a lot of sense.
I suppose with that we should ensure that sources with a sending end like that should take care of automatically removing themselves from the event loop when then realize their sending end has been destroyed and all pending events have been processed?
In any case I'm quite interested with this!
I suppose with that we should ensure that sources with a sending end like that should take care of automatically removing themselves from the event loop when then realize their sending end has been destroyed and all pending events have been processed?
It's funny you should say that, because I was about to open another issue/suggestion about that. It might help to keep it separate, otherwise I think it might become hard to follow.
I wrote an essay over in #78 for you.
I guess I should ask, how would the x11 backend in smithay be brought more in line with this. Currently X11Backend allows you to get X11Handles, effectively acting like the timer sources.
https://github.com/Smithay/smithay/blob/master/src/backend/x11/mod.rs
@i509VCB So firstly, what I'm suggesting is merely a convention for the library, not a trait or anything like that. So in the simplest sense, dependent libraries don't have to change if they don't want to (eg. they don't want to break backwards compatibility, it doesn't suit their architecture).
Having said that, if they wanted to follow the convention, it sounds like the use-case you mention would be to just use a clone_handle()
style method.
what I'm suggesting is merely a convention for the library
Ah I see.
So I've started experimenting a little with that, and I have two things coming to mind I'm not really sure about:
send()
method kind of mixes "real" errors and error caused by the fact that the sender was previously taken, which is kind of weird. Would it make sense to make the method panic if the sender was taken?Channel
, there are actually two different senders (Sender
and SyncSender
) which work with the same receiver. Such an API would force the source type to be duplicated as well.mixes "real" errors and error caused by the fact that the sender was previously taken
Yeah, possibly. I had it that way because, frankly, I tend to panic!()
on send errors anyway or ignore them (it just happens that in my applications' architecture, either they're as critical as method calls, or the failure means something's gone away that I don't have to worry about any more). Since a panic generally means "you, the programmer, made a mistake", your suggestion seems perfectly valid to do. The other alternative is to have a second level of error, and... I can't really think of a use case that makes it worth the extra complexity?
I had missed the detail of the Sender
/SyncSender
, let me think about that.
With https://github.com/Smithay/calloop/pull/89 this point is becoming weaker given the Timer
event source is no longer relevant for that issue, and Channel
and Ping
are already very close in API and would be pretty trivial to unify. We'll need to adjust the book to that though, given the timer event source was used to introduce the pattern. But presenting it as a pattern might be misleading now... :thinking:
There are a few inconsistencies in Calloop's higher-level event sources, and even though they are extremely minor, I thought I'd make the suggestion since I've coded up an alternative for the event sources I've made for ZeroMQ and USB.
Take for example:
Ping
hasmake_ping()
to construct, which returns a(sender, source)
pair. Using it requires callingping()
on the sender.Channel
haschannel()
(notmake_channel()
!) which returns a(sender, source)
pair. Using it requires callingsend()
on the sender.Timer
hasTimer::new()
. Using it requires getting a handle from theTimer
itself.Both
Ping
andChannel
have handles that close on drop.Timer
does not.This all quickly becomes apparent if you have a composite event source that uses multiple kinds of these, and kind of unwieldy at times. For example, if your composite source has both a ping and a channel for internal reasons, you need four fields to use them.
Here is an API we stabilised on that kind of gives the best of both worlds:
Disadvantages:
Advantages:
self.pinger.ping()
self.channel.take_sender()
instead of needing to keep (a) the sender and (b) an option wrapperOption
(send
/clone_sender
/take_sender
) == (map
/clone
/take
)source::Source::new() -> Result<Source>
instead ofsource::make_source() -> Result<(sender, source)>
make_source()
just becomeslet source = Source::new(); (source.take_sender(), source)
Let me know what you think, and if you're interested I'll code something up for the existing types that have sending handles.