minor fix
This commit is contained in:
parent
2595622b37
commit
41e9ae1056
|
|
@ -1,44 +1,53 @@
|
||||||
|
use async_trait::async_trait;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use async_trait::async_trait;
|
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
/// # IManager
|
||||||
/// This is used with all managers to implement multitasking
|
/// 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]
|
#[async_trait]
|
||||||
pub trait IManager {
|
pub trait IManager {
|
||||||
|
|
||||||
/// This defines some setup before the tokio loop is started
|
/// This defines some setup before the tokio loop is started
|
||||||
async fn init(self: &Arc<Self>)
|
async fn init(self: &Arc<Self>)
|
||||||
where
|
where
|
||||||
Self: Send + Sync + 'static
|
Self: Send + Sync + 'static,
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/// this is used to get a future that can be awaited
|
/// this is used to get a future that can be awaited
|
||||||
async fn run(self: &Arc<Self>);
|
async fn run(self: &Arc<Self>);
|
||||||
|
|
||||||
/// This is used to start a future through tokio
|
/// This is used to start a future through tokio
|
||||||
fn start(self: &Arc<Self>)
|
fn start(self: &Arc<Self>)
|
||||||
where
|
where
|
||||||
Self: Send + Sync + 'static
|
Self: Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
let weak_self: Weak<Self> = Arc::downgrade(self);
|
let weak_self: Weak<Self> = Arc::downgrade(self);
|
||||||
|
|
||||||
// this looks horrid but works
|
// this looks horrid but works
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
|
||||||
let weak_self = weak_self.clone();
|
let weak_self = weak_self.clone();
|
||||||
|
|
||||||
let a = weak_self.upgrade().unwrap();
|
let a = weak_self.upgrade().unwrap();
|
||||||
a.init().await;
|
a.init().await;
|
||||||
drop(a);
|
drop(a);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
sleep(Duration::new(1,0)).await;
|
sleep(Duration::new(1, 0)).await;
|
||||||
if let Some(manager) = Weak::upgrade(&weak_self) {
|
if let Some(manager) = Weak::upgrade(&weak_self) {
|
||||||
manager.run().await
|
manager.run().await
|
||||||
} else { () }
|
}
|
||||||
|
()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait Visitor<T: IManager> {
|
||||||
|
fn visit(&self, message: T);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue