From 37056a823a574add2cd452a97f328bb170400a0b Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 4 Jan 2018 19:12:01 +0100 Subject: Return the future on connect instead of adding it to the reactor --- bin/main.rs | 10 ++++------ src/lib.rs | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/bin/main.rs b/bin/main.rs index c9749b6..ac25a93 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -78,7 +78,7 @@ fn main() { // Create an event loop to run the connections on. let mut reactor = Core::new().unwrap(); - let mut task = false; + let mut futures = Vec::new(); // Open a connection and add work for each config for config in configs { @@ -105,11 +105,9 @@ fn main() { } } - bot.connect(&mut reactor, &config).map(|_| task = true); + futures.push(bot.connect(&mut reactor, &config)); } - if task { - // Run the main loop forever - reactor.run(future::empty::<(), ()>()).unwrap(); - } + // Run the bots until they throw an error + reactor.run(future::join_all(futures)).unwrap(); } diff --git a/src/lib.rs b/src/lib.rs index 5592da7..d769075 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,11 +108,12 @@ impl Bot { self.plugins.remove(name) } - /// This connects the `Bot` to IRC and adds a task - /// to the Core that was supplied. + /// This connects the `Bot` to IRC and returns a `Future` + /// which represents the bots work. + /// This `Future` will run forever unless it returns an error. /// - /// You need to run the core, so that frippy - /// can do its work. + /// You need to run the `Future`, so that the `Bot` + /// can actually do its work. /// /// # Examples /// ```no_run @@ -128,11 +129,11 @@ impl Bot { /// let mut reactor = Core::new().unwrap(); /// let mut bot = Bot::new(); /// - /// bot.connect(&mut reactor, &config); - /// reactor.run(future::empty::<(), ()>()).unwrap(); + /// let future = bot.connect(&mut reactor, &config); + /// reactor.run(future).unwrap(); /// # } /// ``` - pub fn connect(&self, reactor: &mut Core, config: &Config) -> Option<()> { + pub fn connect(&self, reactor: &mut Core, config: &Config) -> Option>> { info!("Plugins loaded: {}", self.plugins); let server = @@ -157,13 +158,12 @@ impl Bot { // TODO Verify if we actually need to clone plugins twice let plugins = self.plugins.clone(); - let task = server + let future = server .stream() .for_each(move |message| process_msg(&server, plugins.clone(), message)) .map_err(|e| error!("Failed to process message: {}", e)); - reactor.handle().spawn(task); - Some(()) + Some(Box::new(future)) } } -- cgit v1.2.3-70-g09d2