pub struct Slot<T: Send + Sync + Copy + 'static> { /* private fields */ }
Expand description
Slot component of the Signals and Slots implementation. This uses mpsc channels provided by ‘crossbeam’ internally for communication between this and connected ’Signal’s
Implementations§
Source§impl<T: Send + Sync + Copy + 'static> Slot<T>
impl<T: Send + Sync + Copy + 'static> Slot<T>
Sourcepub fn open(
&mut self,
callback: impl FnMut(T) + 'static + Send + Sync,
) -> Result<(), SlotError>
pub fn open( &mut self, callback: impl FnMut(T) + 'static + Send + Sync, ) -> Result<(), SlotError>
Opens this slot to receiving signals
This spawns a new ‘event loop’ thread for the slot to poll for signals and call the ‘callback’ closure upon receiving them. If the Slot is already open, nothing is done and SlotError::AlradyOpen is returned.
§Arguments
- ‘callback’ - the closure to call upon receiving a signal. Because this is called on a separate thread, objects captured by the closer must be threadsafe (Send + Sync)
§Returns
- success - Ok(())
- failure - Err(SlotError::AlreadyOpen)
Sourcepub fn close(self) -> Result<(), SlotError>
pub fn close(self) -> Result<(), SlotError>
Closes the Slot, so it can no longer receive Signals
This joins the internal ‘event loop’ thread. If the Slot wasn’t open, SlotError::NotOpened is returned. If the Slot was already closed, SlotError::AlreadyClosed is returned. If there was an error joining the thread, SlotError::ThreadJoin(Box<dyn std::any::Any + Send>) is returned. Otherwise, Ok(()) is returned.
§Returns
- success - Ok(())
- failure - SlotError