[][src]Trait heng_rs::Scheduler

pub trait Scheduler: Sized + Send + 'static {
    type Message: Send;
    fn start<F, Fut, R>(
        self,
        time: impl Into<Duration>,
        f: F
    ) -> SharedSchedulerSender<Self::Message>
    where
        F: FnMut(&mut Self, &mut Context<Self>) -> Fut + Send + 'static,
        Fut: Future<Output = R> + Send + 'static
, { ... }
fn run<F, Fut, R>(self, f: F, ctx: Context<Self>)
    where
        F: FnMut(&mut Self, &mut Context<Self>) -> Fut + Send + 'static,
        Fut: Future<Output = R> + Send + 'static
, { ... }
fn handler<'a>(
        &'a mut self,
        _ctx: &'a mut Context<Self>
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> { ... }
fn start_with_handler(
        self,
        time: impl Into<Duration>
    ) -> SharedSchedulerSender<Self::Message> { ... }
fn run_with_handler(self, ctx: Context<Self>) { ... } }

Associated Types

Loading content...

Provided methods

fn start<F, Fut, R>(
    self,
    time: impl Into<Duration>,
    f: F
) -> SharedSchedulerSender<Self::Message> where
    F: FnMut(&mut Self, &mut Context<Self>) -> Fut + Send + 'static,
    Fut: Future<Output = R> + Send + 'static, 

fn run<F, Fut, R>(self, f: F, ctx: Context<Self>) where
    F: FnMut(&mut Self, &mut Context<Self>) -> Fut + Send + 'static,
    Fut: Future<Output = R> + Send + 'static, 

fn handler<'a>(
    &'a mut self,
    _ctx: &'a mut Context<Self>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>

fn start_with_handler(
    self,
    time: impl Into<Duration>
) -> SharedSchedulerSender<Self::Message>

You can access &mut Self and &mut Context<Self> in a future if you manually impl handler method for your Type and start the task with start_with_handler

 use std::pin::Pin;
 use std::future::Future;
 use std::time::{Duration, Instant};

 use heng_rs::{Scheduler, Context};

 struct TestTask {
     field: u32
 }

 impl Scheduler for TestTask {
     type Message = u32;

     fn handler<'a>(&'a mut self, ctx: &'a mut Context<Self>) -> Pin<Box<dyn Future<Output=()> + Send + 'a>> {
         Box::pin(async move {
             assert_eq!(self.field, 0u32);
             // we can modify the context and self in the async block.
             if let Some(msg) = ctx.get_msg_front() {
                 self.field = msg;
             };
             assert_eq!(self.field, 3u32);
         })
     }
 }
 #[tokio::main]
 async fn main() -> std::io::Result<()> {
     let task = TestTask { field: 0 };
     let addr = task.start_with_handler(Duration::from_secs(1));

     // use address to send message to task;
     addr.send(3u32).await;
     tokio::timer::delay(Instant::now() + Duration::from_secs(2)).await;
     Ok(())
 }

fn run_with_handler(self, ctx: Context<Self>)

Loading content...

Implementors

Loading content...