From 41e9ae10560ff9de50c9c957b217837c38c9481a Mon Sep 17 00:00:00 2001 From: michael-bailey Date: Thu, 7 Apr 2022 22:23:01 +0100 Subject: [PATCH] minor fix --- foundation/src/prelude.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/foundation/src/prelude.rs b/foundation/src/prelude.rs index 24654e4..52cba13 100644 --- a/foundation/src/prelude.rs +++ b/foundation/src/prelude.rs @@ -1,44 +1,53 @@ +use async_trait::async_trait; use std::sync::{Arc, Weak}; use std::time::Duration; -use async_trait::async_trait; use tokio::time::sleep; - +/// # IManager /// This is used with all managers to implement multitasking +/// +/// ## Methods +/// - init: gets executed once before a tokio task is created +/// - run: gets called once every tick in a tokio task +/// - start: runs the init function then creates the tokio task for the run function #[async_trait] pub trait IManager { - /// This defines some setup before the tokio loop is started async fn init(self: &Arc) - where - Self: Send + Sync + 'static - {} + where + Self: Send + Sync + 'static, + { + } /// this is used to get a future that can be awaited async fn run(self: &Arc); /// This is used to start a future through tokio fn start(self: &Arc) - where - Self: Send + Sync + 'static + where + Self: Send + Sync + 'static, { let weak_self: Weak = Arc::downgrade(self); // this looks horrid but works tokio::spawn(async move { - let weak_self = weak_self.clone(); - let a = weak_self.upgrade().unwrap(); + let a = weak_self.upgrade().unwrap(); a.init().await; drop(a); loop { - sleep(Duration::new(1,0)).await; + sleep(Duration::new(1, 0)).await; if let Some(manager) = Weak::upgrade(&weak_self) { manager.run().await - } else { () } + } + () } }); } -} \ No newline at end of file +} + +trait Visitor { + fn visit(&self, message: T); +}