From 00cd186c63f68c1521cc3c4233b5d9bf0fb14e8d Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Tue, 20 Dec 2022 23:26:27 +0100 Subject: [PATCH] feat(rt): implement runtime trait to define a unique runtime Signed-off-by: Vincenzo Palazzo --- .gitsigners | 1 + examples/rio_triage_await/src/main.rs | 8 ++++---- rt/src/lib.rs | 6 +++++- rt/src/rt.rs | 21 +++++++++++++++++++++ rt/src/runitime.rs | 19 +++++++++++++------ 5 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 .gitsigners create mode 100644 rt/src/rt.rs diff --git a/.gitsigners b/.gitsigners new file mode 100644 index 0000000..a09221f --- /dev/null +++ b/.gitsigners @@ -0,0 +1 @@ +hyyfiw7gfnqfmgf8df6b6mu8pg6bka3end4kyxrtyekmd35ftzusq4 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAtadMUTirMU4y+D5cztN4KsZQIelAeSIEKWPOyxvOzt diff --git a/examples/rio_triage_await/src/main.rs b/examples/rio_triage_await/src/main.rs index ba673bb..dc850e9 100644 --- a/examples/rio_triage_await/src/main.rs +++ b/examples/rio_triage_await/src/main.rs @@ -6,7 +6,8 @@ use github::model::NewIssue; use hackmd::api::HackmdAPI; use hackmd::model::NewNote; use log::debug; -use rio_rt::runitime as rio; +use rio_rt::runitime::Runtime; +use rio_rt::Rt; use surf; pub(crate) mod extractor; @@ -29,6 +30,7 @@ async fn run( fn main() { env_logger::init(); + let rio = Runtime::new(); // FIXME: load conf from json let conf = model::TriageConf { @@ -44,9 +46,7 @@ fn main() { let github = github::GithubExtractor::new(&conf); let hackmd_api = hackmd::api::HackmdAPI::new("", false); - rio::block_on(async move { + rio.block_on(async move { run(&github, &hackmd_api).await.unwrap(); }); - - rio::wait(); } diff --git a/rt/src/lib.rs b/rt/src/lib.rs index 22e9e5e..76ee6e4 100644 --- a/rt/src/lib.rs +++ b/rt/src/lib.rs @@ -6,7 +6,11 @@ //! //! In addition, from this crate the user can consider to learn more on the async //! programming, because there is a lot to learn and a lot to contribute. -// TODO: move feature inside a std :) #![feature(once_cell)] +#[allow(unused_imports)] +#[allow(incomplete_features)] +mod rt; pub mod runitime; pub mod task; + +pub use rt::Rt; diff --git a/rt/src/rt.rs b/rt/src/rt.rs new file mode 100644 index 0000000..4ef1d0a --- /dev/null +++ b/rt/src/rt.rs @@ -0,0 +1,21 @@ +//! A reference implementation of +//! the async main and tests proposed +//! discussed at https://rust-lang.zulipchat.com/#narrow/stream/187312-wg-async/topic/Weekly.20sync.202022-12-15 +use std::future::Future; + +/// Runtime trait definition is a +/// generic interface of the trait +/// that allow to have a generic +/// interface. +/// +/// This allow the possibility to start +/// experimenting with this trait and +/// see if it is possible design library +/// executor agnostics. +pub trait Rt { + fn new() -> &'static Self; + + /// Allow to run the future and block the execution till this + /// future is ended. + fn block_on(&self, future: impl Future + Send + 'static); +} diff --git a/rt/src/runitime.rs b/rt/src/runitime.rs index 2ba14e7..f4f70fc 100644 --- a/rt/src/runitime.rs +++ b/rt/src/runitime.rs @@ -11,12 +11,13 @@ use std::task::Poll; use log::debug; +use crate::rt::Rt; use crate::task::Task; pub(crate) type Queue = Arc>>>; /// Runtime definition -pub(crate) struct Runtime { +pub struct Runtime { pub(crate) task_queue: Queue, /// Size of the runtime pub(crate) size: AtomicUsize, @@ -92,14 +93,20 @@ fn configure() -> Runtime { pub fn spawn(future: impl Future + Send + 'static) { Runtime::get().spawn(future); } -/// Block on a `Future` and stop others on the `whorl` runtime until this -/// one completes. -pub fn block_on(future: impl Future + Send + 'static) { - Runtime::get().spawn_blocking(future); -} + /// Block further execution of a program until all of the tasks on the /// `whorl` runtime are completed. pub fn wait() { let runtime = Runtime::get(); while runtime.size.load(Ordering::Relaxed) > 0 {} } + +impl Rt for Runtime { + fn new() -> &'static Self { + Runtime::get() + } + + fn block_on(&self, future: impl Future + Send + 'static) { + self.spawn_blocking(future) + } +}