Enum calloop::transient::TransientSource
source · pub enum TransientSource<T> {
Keep(T),
Register(T),
Disable(T),
Remove(T),
None,
}
Expand description
A TransientSource
wraps a Calloop event source and manages its
registration. A user of this type only needs to perform the usual Calloop
calls (process_events()
and *register()
) and the return value of
process_events()
.
Rather than needing to check for the full set of
PostAction
values returned from process_events()
,
you can just check for Continue
or Reregister
and pass that back out
through your own process_events()
implementation. In your registration
functions, you then only need to call the same function on this type ie.
register()
inside register()
etc.
For example, say you have a source that contains a channel along with some other logic. If the channel’s sending end has been dropped, it needs to be removed from the loop. So to manage this, you use this in your struct:
struct CompositeSource {
// Event source for channel.
mpsc_receiver: TransientSource<calloop::channel::Channel<T>>,
// Any other fields go here...
}
To create the transient source, you can simply use the Into
implementation:
let (sender, source) = channel();
let mpsc_receiver: TransientSource<Channel> = source.into();
TransientSource
implements EventSource
and passes
through process_events()
calls, so in the parent’s process_events()
implementation you can just do this:
fn process_events<F>(
&mut self,
readiness: calloop::Readiness,
token: calloop::Token,
callback: F,
) -> Result<calloop::PostAction, Self::Error>
where
F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
{
let channel_return = self.mpsc_receiver.process_events(readiness, token, callback)?;
// Perform other logic here...
Ok(channel_return)
}
Note that:
-
You can call
process_events()
on theTransientSource<Channel>
even if the channel has been unregistered and dropped. All that will happen is that you won’t get any events from it. -
The
PostAction
returned fromprocess_events()
will only ever bePostAction::Continue
orPostAction::Reregister
. You will still need to combine this with the result of any other sources (transient or not).
Once you return channel_return
from your process_events()
method (and
assuming it propagates all the way up to the event loop itself through any
other event sources), the event loop might call reregister()
on your
source. All your source has to do is:
fn reregister(
&mut self,
poll: &mut calloop::Poll,
token_factory: &mut calloop::TokenFactory,
) -> crate::Result<()> {
self.mpsc_receiver.reregister(poll, token_factory)?;
// Other registration actions...
Ok(())
}
The TransientSource
will take care of updating the registration of the
inner source, even if it actually needs to be unregistered or initially
registered.
Variants§
Keep(T)
The source should be kept in the loop.
Register(T)
The source needs to be registered with the loop.
Disable(T)
The source needs to be disabled but kept.
Remove(T)
The source needs to be removed from the loop.
None
The source has been removed from the loop and dropped (this might also be observed if there is a panic while changing states).
Implementations§
Trait Implementations§
source§impl<T: Debug> Debug for TransientSource<T>
impl<T: Debug> Debug for TransientSource<T>
source§impl<T: EventSource> EventSource for TransientSource<T>
impl<T: EventSource> EventSource for TransientSource<T>
§type Event = <T as EventSource>::Event
type Event = <T as EventSource>::Event
§type Metadata = <T as EventSource>::Metadata
type Metadata = <T as EventSource>::Metadata
§type Ret = <T as EventSource>::Ret
type Ret = <T as EventSource>::Ret
§type Error = <T as EventSource>::Error
type Error = <T as EventSource>::Error
process_events()
(not the user callback!).