Struct TransientSource

pub struct TransientSource<T> { /* private fields */ }
Available on crate feature wayland and Linux only.
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();

(If you want to start off with an empty TransientSource, you can just use Default::default() instead.)

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 the TransientSource<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 from process_events() will only ever be PostAction::Continue or PostAction::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.

§Replacing or removing TransientSources

Not properly removing or replacing TransientSources can cause spurious wakeups of the event loop, and in some cases can leak file descriptors or fail to free entries in Calloop’s internal data structures. No unsoundness or undefined behaviour will result, but leaking file descriptors can result in errors or panics.

If you want to remove a source before it returns PostAction::Remove, use the TransientSource::remove() method. If you want to replace a source with another one, use the TransientSource::replace() method. Either of these may be called at any time during processing or from outside the event loop. Both require either returning PostAction::Reregister from the process_event() call that does this, or reregistering the event source some other way eg. via the top-level loop handle.

If, instead, you directly assign a new source to the variable holding the TransientSource, the inner source will be dropped before it can be unregistered. For example:

self.mpsc_receiver = Default::default();
self.mpsc_receiver = new_channel.into();

Implementations§

§

impl<T> TransientSource<T>

pub fn map<F, U>(&mut self, f: F) -> Option<U>
where F: FnOnce(&mut T) -> U,

Apply a function to the enclosed source, if it exists and is not about to be removed.

pub fn is_none(&self) -> bool

Returns true if there is no wrapped event source.

pub fn remove(&mut self)

Removes the wrapped event source from the event loop and this wrapper.

If this is called from outside of the event loop, you will need to wake up the event loop for any changes to take place. If it is called from within the event loop, you must return PostAction::Reregister from your own event source’s process_events(), and the source will be unregistered as needed after it exits.

pub fn replace(&mut self, new: T)

Replace the currently wrapped source with the given one. No more events will be generated from the old source after this point. The old source will not be dropped immediately, it will be kept so that it can be deregistered.

If this is called from outside of the event loop, you will need to wake up the event loop for any changes to take place. If it is called from within the event loop, you must return PostAction::Reregister from your own event source’s process_events(), and the sources will be registered and unregistered as needed after it exits.

Trait Implementations§

§

impl<T> Debug for TransientSource<T>
where T: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T> Default for TransientSource<T>
where T: Default,

§

fn default() -> TransientSource<T>

Returns the “default value” for a type. Read more
§

impl<T> EventSource for TransientSource<T>
where T: EventSource,

§

type Event = <T as EventSource>::Event

The type of events generated by your source.
§

type Metadata = <T as EventSource>::Metadata

Some metadata of your event source Read more
§

type Ret = <T as EventSource>::Ret

The return type of the user callback Read more
§

type Error = <T as EventSource>::Error

The error type returned from process_events() (not the user callback!).
§

fn process_events<F>( &mut self, readiness: Readiness, token: Token, callback: F, ) -> Result<PostAction, <TransientSource<T> as EventSource>::Error>

Process any relevant events Read more
§

fn register( &mut self, poll: &mut Poll, token_factory: &mut TokenFactory, ) -> Result<(), Error>

Register yourself to this poll instance Read more
§

fn reregister( &mut self, poll: &mut Poll, token_factory: &mut TokenFactory, ) -> Result<(), Error>

Re-register your file descriptors Read more
§

fn unregister(&mut self, poll: &mut Poll) -> Result<(), Error>

Unregister your file descriptors Read more
§

const NEEDS_EXTRA_LIFECYCLE_EVENTS: bool = false

Whether this source needs to be sent the EventSource::before_sleep and EventSource::before_handle_events notifications. These are opt-in because they require more expensive checks, and almost all sources will not need these notifications
§

fn before_sleep(&mut self) -> Result<Option<(Readiness, Token)>, Error>

Notification that a single poll is about to begin Read more
§

fn before_handle_events(&mut self, events: EventIterator<'_>)

Notification that polling is complete, and EventSource::process_events will be called with the given events for this source. The iterator may be empty, which indicates that no events were generated for this source Read more
§

impl<T> From<T> for TransientSource<T>
where T: EventSource,

§

fn from(source: T) -> TransientSource<T>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for TransientSource<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for TransientSource<T>
where T: RefUnwindSafe,

§

impl<T> Send for TransientSource<T>
where T: Send,

§

impl<T> Sync for TransientSource<T>
where T: Sync,

§

impl<T> Unpin for TransientSource<T>
where T: Unpin,

§

impl<T> UnwindSafe for TransientSource<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

Source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

Convert the source color to the destination color using the specified method.
Source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default.
§

impl<T> Also for T

§

fn also<F>(self, block: F) -> Self
where F: FnOnce(&mut Self),

Apply a function to this value and return the (possibly) modified value.
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T, Res> Apply<Res> for T
where T: ?Sized,

§

fn apply<F>(self, f: F) -> Res
where F: FnOnce(Self) -> Res, Self: Sized,

Apply a function which takes the parameter by value.
§

fn apply_ref<F>(&self, f: F) -> Res
where F: FnOnce(&Self) -> Res,

Apply a function which takes the parameter by reference.
§

fn apply_mut<F>(&mut self, f: F) -> Res
where F: FnOnce(&mut Self) -> Res,

Apply a function which takes the parameter by mutable reference.
Source§

impl<T, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

Source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
Source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

Source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

Source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

Source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromAngle<T> for T

Source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
Source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

Source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> IntoAngle<U> for T
where U: FromAngle<T>,

Source§

fn into_angle(self) -> U

Performs a conversion into T.
§

impl<State, Message> IntoBoot<State, Message> for State

§

fn into_boot(self) -> (State, Task<Message>)

Turns some type into the initial state of some Application.
Source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

Source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

Source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
Source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

Source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
Source§

impl<T> IntoStimulus<T> for T

Source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

Source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
Source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

Source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
Source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

Source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
Source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

Source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> MaybeClone for T

§

impl<T> MaybeDebug for T

§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> MaybeSync for T
where T: Sync,