diff --git a/src/loop_logic.rs b/src/loop_logic.rs index 091d911d..a0ab8e5e 100644 --- a/src/loop_logic.rs +++ b/src/loop_logic.rs @@ -58,7 +58,7 @@ pub struct RegistrationToken { pub(crate) struct LoopInner<'l, Data> { pub(crate) poll: RefCell, pub(crate) sources: RefCell + 'l>>>, - pub(crate) sources_with_additional_lifetime_events: AdditionalLifetimeEventsSet, + pub(crate) sources_with_additional_lifecycle_events: AdditionalLifetimeEventsSet, idles: RefCell>>, pending_action: Cell, } @@ -141,7 +141,7 @@ impl<'l, Data> LoopHandle<'l, Data> { let ret = sources.get(key).unwrap().register( &mut poll, self.inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(RegistrationToken { key }), &mut TokenFactory::new(key), ); @@ -179,7 +179,7 @@ impl<'l, Data> LoopHandle<'l, Data> { source.register( &mut self.inner.poll.borrow_mut(), self.inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(*token), &mut TokenFactory::new(token.key), )?; @@ -196,7 +196,7 @@ impl<'l, Data> LoopHandle<'l, Data> { if !source.reregister( &mut self.inner.poll.borrow_mut(), self.inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(*token), &mut TokenFactory::new(token.key), )? { @@ -215,7 +215,7 @@ impl<'l, Data> LoopHandle<'l, Data> { if !source.unregister( &mut self.inner.poll.borrow_mut(), self.inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(*token), )? { // we are in a callback, store for later processing @@ -231,7 +231,7 @@ impl<'l, Data> LoopHandle<'l, Data> { if let Err(e) = source.unregister( &mut self.inner.poll.borrow_mut(), self.inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(token), ) { log::warn!( @@ -293,7 +293,7 @@ impl<'l, Data> EventLoop<'l, Data> { sources: RefCell::new(Slab::new()), idles: RefCell::new(Vec::new()), pending_action: Cell::new(PostAction::Continue), - sources_with_additional_lifetime_events: Default::default(), + sources_with_additional_lifecycle_events: Default::default(), }), }; @@ -323,7 +323,7 @@ impl<'l, Data> EventLoop<'l, Data> { let mut extra_lifecycle_sources = self .handle .inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .values .borrow_mut(); let sources = &self.handle.inner.sources.borrow(); @@ -366,7 +366,7 @@ impl<'l, Data> EventLoop<'l, Data> { let mut extra_lifecycle_sources = self .handle .inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .values .borrow_mut(); if !extra_lifecycle_sources.is_empty() { @@ -416,7 +416,7 @@ impl<'l, Data> EventLoop<'l, Data> { &mut self.handle.inner.poll.borrow_mut(), self.handle .inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(RegistrationToken { key: registroken_token, }), @@ -428,7 +428,7 @@ impl<'l, Data> EventLoop<'l, Data> { &mut self.handle.inner.poll.borrow_mut(), self.handle .inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(RegistrationToken { key: registroken_token, }), @@ -458,7 +458,7 @@ impl<'l, Data> EventLoop<'l, Data> { &mut poll, self.handle .inner - .sources_with_additional_lifetime_events + .sources_with_additional_lifecycle_events .create_register_for_token(RegistrationToken { key: registroken_token, }), diff --git a/src/sources/mod.rs b/src/sources/mod.rs index cab85015..4ccc1d5c 100644 --- a/src/sources/mod.rs +++ b/src/sources/mod.rs @@ -286,7 +286,7 @@ impl EventSource for &mut T { pub(crate) struct DispatcherInner { source: S, callback: F, - needs_additional_lifetime_events: bool, + needs_additional_lifecycle_events: bool, } impl EventDispatcher for RefCell> @@ -314,13 +314,13 @@ where fn register( &self, poll: &mut Poll, - mut additional_lifetime_register: AdditionalLifetimeEventsRegister<'_>, + mut additional_lifecycle_register: AdditionalLifetimeEventsRegister<'_>, token_factory: &mut TokenFactory, ) -> crate::Result<()> { let mut this = self.borrow_mut(); - if this.needs_additional_lifetime_events { - additional_lifetime_register.register(); + if this.needs_additional_lifecycle_events { + additional_lifecycle_register.register(); } this.source.register(poll, token_factory) } @@ -328,13 +328,13 @@ where fn reregister( &self, poll: &mut Poll, - mut additional_lifetime_register: AdditionalLifetimeEventsRegister<'_>, + mut additional_lifecycle_register: AdditionalLifetimeEventsRegister<'_>, token_factory: &mut TokenFactory, ) -> crate::Result { if let Ok(mut me) = self.try_borrow_mut() { me.source.reregister(poll, token_factory)?; - if me.needs_additional_lifetime_events { - additional_lifetime_register.register(); + if me.needs_additional_lifecycle_events { + additional_lifecycle_register.register(); } Ok(true) } else { @@ -345,12 +345,12 @@ where fn unregister( &self, poll: &mut Poll, - mut additional_lifetime_register: AdditionalLifetimeEventsRegister<'_>, + mut additional_lifecycle_register: AdditionalLifetimeEventsRegister<'_>, ) -> crate::Result { if let Ok(mut me) = self.try_borrow_mut() { me.source.unregister(poll)?; - if me.needs_additional_lifetime_events { - additional_lifetime_register.unregister(); + if me.needs_additional_lifecycle_events { + additional_lifecycle_register.unregister(); } Ok(true) } else { @@ -382,21 +382,21 @@ pub(crate) trait EventDispatcher { fn register( &self, poll: &mut Poll, - additional_lifetime_register: AdditionalLifetimeEventsRegister<'_>, + additional_lifecycle_register: AdditionalLifetimeEventsRegister<'_>, token_factory: &mut TokenFactory, ) -> crate::Result<()>; fn reregister( &self, poll: &mut Poll, - additional_lifetime_register: AdditionalLifetimeEventsRegister<'_>, + additional_lifecycle_register: AdditionalLifetimeEventsRegister<'_>, token_factory: &mut TokenFactory, ) -> crate::Result; fn unregister( &self, poll: &mut Poll, - additional_lifetime_register: AdditionalLifetimeEventsRegister<'_>, + additional_lifecycle_register: AdditionalLifetimeEventsRegister<'_>, ) -> crate::Result; fn before_sleep(&self) -> crate::Result>; @@ -404,7 +404,11 @@ pub(crate) trait EventDispatcher { } #[derive(Default)] +/// The list of events pub(crate) struct AdditionalLifetimeEventsSet { + /// The list of events. The boolean indicates whether the source had an event + /// - this is stored in this list because we need to know this value for each + /// item at once - that is, to avoid allocating in the hot loop pub(crate) values: RefCell>, } @@ -417,6 +421,9 @@ impl AdditionalLifetimeEventsSet { } } +/// To ensure that registering the events which need additional lifecycle events +/// happens correctly, we do this within the implementations of `EventDispatcher` +/// `AdditionalLifetimeEventsRegister` is a helper type to centralise this logic pub(crate) struct AdditionalLifetimeEventsRegister<'a> { set: &'a AdditionalLifetimeEventsSet, token: RegistrationToken, @@ -497,7 +504,7 @@ where Dispatcher(Rc::new(RefCell::new(DispatcherInner { source, callback, - needs_additional_lifetime_events: S::NEEDS_EXTRA_LIFECYCLE_EVENTS, + needs_additional_lifecycle_events: S::NEEDS_EXTRA_LIFECYCLE_EVENTS, }))) }