From 8e3b8834c0326f40f654f5a5f94fd1b5a319b427 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 2 Dec 2020 11:46:46 +0100 Subject: [PATCH] Async actor runs now --- src/actor.rs | 4 ++-- src/main.rs | 9 +++++++-- src/registries/actuators.rs | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 8a042e8..b6e37f9 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -26,7 +26,7 @@ pub struct Actor { inner: Option, actuator: Box, - future: Option + Unpin + Send>>, + future: Option>, } impl Actor { @@ -80,7 +80,7 @@ impl + Unpin> Future for Actor { } }, Some(Poll::Ready(Some(state))) => { - this.actuator.apply(state); + this.future.replace(this.actuator.apply(state)); Poll::Pending } } diff --git a/src/main.rs b/src/main.rs index 68e5c8d..2120eb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -151,11 +151,16 @@ fn maybe(matches: clap::ArgMatches, log: Arc) -> Result<(), Error> { // TODO HERE: Spawn all actors & inits // Like so - ex.spawn(actor); + let t = ex.spawn(actor); let (signal, shutdown) = async_channel::bounded::<()>(1); easy_parallel::Parallel::new() - .each(0..4, |_| smol::block_on(ex.run(shutdown.recv()))); + .each(0..4, |_| smol::block_on(ex.run(shutdown.recv()))) + .run(); + + smol::block_on(t); + + let db = db::Databases::new(&log, &config)?; // TODO: Spawn api connections on their own (non-main) thread, use the main thread to diff --git a/src/registries/actuators.rs b/src/registries/actuators.rs index fc84f0f..be561dd 100644 --- a/src/registries/actuators.rs +++ b/src/registries/actuators.rs @@ -6,6 +6,7 @@ use smol::lock::RwLock; use std::pin::Pin; use futures::ready; use futures::prelude::*; +use futures::future::BoxFuture; use futures::channel::mpsc; use futures::task::{Context, Poll, Spawn}; use futures_signals::signal::Signal; @@ -15,13 +16,14 @@ use crate::db::machine::MachineState; use std::collections::HashMap; pub trait Actuator { - fn apply(&mut self, state: MachineState); + fn apply(&mut self, state: MachineState) -> BoxFuture<'static, ()>; } pub struct Dummy; impl Actuator for Dummy { - fn apply(&mut self, state: MachineState) { + fn apply(&mut self, state: MachineState) -> BoxFuture<'static, ()> { println!("New state for dummy actuator: {:?}", state); + Box::pin(smol::future::ready(())) } }