doc improvements and some more renaming
Some checks failed
Rust/sat-rs/pipeline/pr-main There was a failure building this commit

This commit is contained in:
Robin Müller 2024-02-23 11:27:13 +01:00
parent dcfd4dfddd
commit 90ab105504
Signed by: muellerr
GPG Key ID: A649FB78196E3849

View File

@ -29,8 +29,8 @@
//! 3. The event manager receives the receiver component as part of a [EventReceiveProvider] //! 3. The event manager receives the receiver component as part of a [EventReceiveProvider]
//! implementation so all events are routed to the manager. //! implementation so all events are routed to the manager.
//! 4. Create the [send event providers][EventSendProvider]s which allow routing events to //! 4. Create the [send event providers][EventSendProvider]s which allow routing events to
//! subscribers. You can now use their [sender IDs][EventSendProvider::id] to subscribe for //! subscribers. You can now use their [sender IDs][EventSendProvider::channel_id] to subscribe
//! event groups, for example by using the [EventManager::subscribe_single] method. //! for event groups, for example by using the [EventManager::subscribe_single] method.
//! 5. Add the send provider as well using the [EventManager::add_sender] call so the event //! 5. Add the send provider as well using the [EventManager::add_sender] call so the event
//! manager can route listener groups to a the send provider. //! manager can route listener groups to a the send provider.
//! //!
@ -91,7 +91,7 @@ pub trait EventReceiveProvider<Event: GenericEvent, AuxDataProvider = Params> {
/// To allow returning arbitrary additional auxiliary data, a mutable slice is passed to the /// To allow returning arbitrary additional auxiliary data, a mutable slice is passed to the
/// [Self::receive] call as well. Receivers can write data to this slice, but care must be taken /// [Self::receive] call as well. Receivers can write data to this slice, but care must be taken
/// to avoid panics due to size missmatches or out of bound writes. /// to avoid panics due to size missmatches or out of bound writes.
fn receive(&self) -> Option<(Event, Option<AuxDataProvider>)>; fn try_recv_event(&self) -> Option<(Event, Option<AuxDataProvider>)>;
} }
pub trait ListenerMapProvider { pub trait ListenerMapProvider {
@ -236,6 +236,7 @@ impl<
} }
} }
/// Add a new sender component which can be used to send events to subscribers.
pub fn add_sender(&mut self, send_provider: SP) { pub fn add_sender(&mut self, send_provider: SP) {
if !self if !self
.sender_map .sender_map
@ -245,6 +246,7 @@ impl<
} }
} }
/// Generic function to update the event subscribers.
fn update_listeners(&mut self, key: ListenerKey, sender_id: ChannelId) { fn update_listeners(&mut self, key: ListenerKey, sender_id: ChannelId) {
self.listener_map.add_listener(key, sender_id); self.listener_map.add_listener(key, sender_id);
} }
@ -287,7 +289,7 @@ impl<
} }
} }
}; };
if let Some((event, aux_data)) = self.event_receiver.receive() { if let Some((event, aux_data)) = self.event_receiver.try_recv_event() {
let single_key = ListenerKey::Single(event.raw_as_largest_type()); let single_key = ListenerKey::Single(event.raw_as_largest_type());
send_handler(&single_key, event, &aux_data); send_handler(&single_key, event, &aux_data);
let group_key = ListenerKey::Group(event.group_id_as_largest_type()); let group_key = ListenerKey::Group(event.group_id_as_largest_type());
@ -339,31 +341,14 @@ pub mod alloc_mod {
} }
} }
/// Default listener map.
///
/// Simple implementation which uses a [HashMap] and a [Vec] internally.
#[derive(Default)] #[derive(Default)]
pub struct DefaultListenerMap { pub struct DefaultListenerMap {
listeners: HashMap<ListenerKey, Vec<ChannelId>>, listeners: HashMap<ListenerKey, Vec<ChannelId>>,
} }
pub struct DefaultSenderMap<
SP: EventSendProvider<EV, AUX>,
EV: GenericEvent = EventU32,
AUX = Params,
> {
senders: HashMap<ChannelId, SP>,
phantom: PhantomData<(EV, AUX)>,
}
impl<SP: EventSendProvider<EV, AUX>, EV: GenericEvent, AUX> Default
for DefaultSenderMap<SP, EV, AUX>
{
fn default() -> Self {
Self {
senders: Default::default(),
phantom: Default::default(),
}
}
}
impl ListenerMapProvider for DefaultListenerMap { impl ListenerMapProvider for DefaultListenerMap {
fn get_listeners(&self) -> Vec<ListenerKey> { fn get_listeners(&self) -> Vec<ListenerKey> {
let mut key_list = Vec::new(); let mut key_list = Vec::new();
@ -399,6 +384,29 @@ pub mod alloc_mod {
} }
} }
/// Default sender map.
///
/// Simple implementation which uses a [HashMap] internally.
pub struct DefaultSenderMap<
SP: EventSendProvider<EV, AUX>,
EV: GenericEvent = EventU32,
AUX = Params,
> {
senders: HashMap<ChannelId, SP>,
phantom: PhantomData<(EV, AUX)>,
}
impl<SP: EventSendProvider<EV, AUX>, EV: GenericEvent, AUX> Default
for DefaultSenderMap<SP, EV, AUX>
{
fn default() -> Self {
Self {
senders: Default::default(),
phantom: Default::default(),
}
}
}
impl<SP: EventSendProvider<EV, AUX>, EV: GenericEvent, AUX> SenderMapProvider<SP, EV, AUX> impl<SP: EventSendProvider<EV, AUX>, EV: GenericEvent, AUX> SenderMapProvider<SP, EV, AUX>
for DefaultSenderMap<SP, EV, AUX> for DefaultSenderMap<SP, EV, AUX>
{ {
@ -440,7 +448,7 @@ pub mod std_mod {
} }
} }
impl<Event: GenericEvent + Send> EventReceiveProvider<Event> for MpscEventReceiver<Event> { impl<Event: GenericEvent + Send> EventReceiveProvider<Event> for MpscEventReceiver<Event> {
fn receive(&self) -> Option<EventWithAuxData<Event>> { fn try_recv_event(&self) -> Option<EventWithAuxData<Event>> {
if let Ok(event_and_data) = self.mpsc_receiver.try_recv() { if let Ok(event_and_data) = self.mpsc_receiver.try_recv() {
return Some(event_and_data); return Some(event_and_data);
} }