From 67f7d801fab669ad80d10cfbe7e548e8041bb286 Mon Sep 17 00:00:00 2001 From: ennucore Date: Sun, 21 Nov 2021 00:08:47 +0300 Subject: [PATCH] `Transport.main_loop_iteration()` --- src/transport.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index 173de39..ab5e3c1 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -41,6 +41,10 @@ pub struct Transport { impl Transport { /// Create new transport with given interfaces pub fn new(interfaces: Vec>) -> Self { + #[cfg(not(std))] + if interfaces.iter().map(|interface| interface.has_blocking_main() as u8).sum::() > 1 { + panic!("There is two interfaces with blocking main loops and we have no threads because this is no_std!"); + } Self { interfaces, peers: vec![] } } @@ -123,6 +127,21 @@ impl Transport { Some((msg, self.find_or_add_peer(interface_id, peer_data))) } else { None } } + + /// Run one iteration of the main loop + pub fn main_loop_iteration(&mut self) -> IFResult<()> { + #[cfg(std)] + self.interfaces.par_iter_mut().map(|interface| interface.main_loop_iteration()).collect::>()?; + #[cfg(not(std))] + { + self.interfaces.iter_mut().map(|interface| if !interface.has_blocking_main() { interface.main_loop_iteration() } else { Ok(()) }).collect::>()?; + let blocking_interface_index = self.interfaces.iter().position(|interface| interface.has_blocking_main()); + if let Some(ind) = blocking_interface_index { + self.interfaces[ind].main_loop_iteration()?; + } + } + Ok(()) + } } #[cfg(test)]