From c03d869694c87e6d72d0686e360344c94fb1c3f3 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 6 Mar 2019 10:24:58 -0800 Subject: [PATCH] return io::Result from run method, remove Handle --- actix-rt/CHANGES.md | 6 ++++ actix-rt/Cargo.toml | 4 +-- actix-rt/src/builder.rs | 21 +++++++++---- actix-rt/src/lib.rs | 2 +- actix-rt/src/runtime.rs | 66 ++--------------------------------------- actix-rt/src/system.rs | 3 +- 6 files changed, 28 insertions(+), 74 deletions(-) diff --git a/actix-rt/CHANGES.md b/actix-rt/CHANGES.md index 14e1cde7..83d2b73f 100644 --- a/actix-rt/CHANGES.md +++ b/actix-rt/CHANGES.md @@ -1,5 +1,11 @@ # Changes +## [0.2.0] - 2019-03-06 + +* `run` method returns `io::Result<()>` + +* Removed `Handle` + ## [0.1.0] - 2018-12-09 * Initial release diff --git a/actix-rt/Cargo.toml b/actix-rt/Cargo.toml index eec0f4a3..f89eda05 100644 --- a/actix-rt/Cargo.toml +++ b/actix-rt/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-rt" -version = "0.1.0" +version = "0.2.0" authors = ["Nikolay Kim "] description = "Actix runtime" keywords = ["network", "framework", "async", "futures"] @@ -20,7 +20,7 @@ path = "src/lib.rs" [dependencies] log = "0.4" bytes = "0.4" -futures = "0.1.24" +futures = "0.1.25" tokio-current-thread = "0.1" tokio-executor = "0.1.5" tokio-reactor = "0.1.7" diff --git a/actix-rt/src/builder.rs b/actix-rt/src/builder.rs index 3be9fb77..73bfaa36 100644 --- a/actix-rt/src/builder.rs +++ b/actix-rt/src/builder.rs @@ -72,7 +72,7 @@ impl Builder { /// This function will start tokio runtime and will finish once the /// `System::stop()` message get called. /// Function `f` get called within tokio runtime context. - pub fn run(self, f: F) -> i32 + pub fn run(self, f: F) -> io::Result<()> where F: FnOnce() + 'static, { @@ -140,7 +140,7 @@ pub struct SystemRunner { impl SystemRunner { /// This function will start event loop and will finish once the /// `System::stop()` function is called. - pub fn run(self) -> i32 { + pub fn run(self) -> io::Result<()> { let SystemRunner { mut rt, stop, .. } = self; // run loop @@ -148,12 +148,21 @@ impl SystemRunner { Arbiter::run_system(); Ok::<_, ()>(()) })); - let code = match rt.block_on(stop) { - Ok(code) => code, - Err(_) => 1, + let result = match rt.block_on(stop) { + Ok(code) => { + if code != 0 { + Err(io::Error::new( + io::ErrorKind::Other, + format!("Non-zero exit code: {}", code), + )) + } else { + Ok(()) + } + } + Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)), }; Arbiter::stop_system(); - code + result } /// Execute a future and wait for result. diff --git a/actix-rt/src/lib.rs b/actix-rt/src/lib.rs index c2db6caf..3bfbb7b2 100644 --- a/actix-rt/src/lib.rs +++ b/actix-rt/src/lib.rs @@ -7,7 +7,7 @@ mod system; pub use self::arbiter::Arbiter; pub use self::builder::{Builder, SystemRunner}; -pub use self::runtime::{Handle, Runtime}; +pub use self::runtime::Runtime; pub use self::system::System; /// Spawns a future on the current arbiter. diff --git a/actix-rt/src/runtime.rs b/actix-rt/src/runtime.rs index fac0b9d3..e5269141 100644 --- a/actix-rt/src/runtime.rs +++ b/actix-rt/src/runtime.rs @@ -1,9 +1,7 @@ use std::error::Error; -use std::fmt; -use std::io; +use std::{fmt, io}; -use futures::{future, Future}; -use tokio_current_thread::Handle as ExecutorHandle; +use futures::Future; use tokio_current_thread::{self as current_thread, CurrentThread}; use tokio_executor; use tokio_reactor::{self, Reactor}; @@ -26,58 +24,6 @@ pub struct Runtime { executor: CurrentThread>, } -/// Handle to spawn a future on the corresponding `CurrentThread` runtime instance -#[derive(Debug, Clone)] -pub struct Handle(ExecutorHandle); - -impl Handle { - /// Spawn a future onto the `CurrentThread` runtime instance corresponding to this handle - /// - /// # Panics - /// - /// This function panics if the spawn fails. Failure occurs if the `CurrentThread` - /// instance of the `Handle` does not exist anymore. - pub fn spawn(&self, future: F) -> Result<(), tokio_executor::SpawnError> - where - F: Future + Send + 'static, - { - self.0.spawn(future) - } - - /// Provides a best effort **hint** to whether or not `spawn` will succeed. - /// - /// This function may return both false positives **and** false negatives. - /// If `status` returns `Ok`, then a call to `spawn` will *probably* - /// succeed, but may fail. If `status` returns `Err`, a call to `spawn` will - /// *probably* fail, but may succeed. - /// - /// This allows a caller to avoid creating the task if the call to `spawn` - /// has a high likelihood of failing. - pub fn status(&self) -> Result<(), tokio_executor::SpawnError> { - self.0.status() - } -} - -impl future::Executor for Handle -where - T: Future + Send + 'static, -{ - fn execute(&self, future: T) -> Result<(), future::ExecuteError> { - if let Err(e) = self.status() { - let kind = if e.is_at_capacity() { - future::ExecuteErrorKind::NoCapacity - } else { - future::ExecuteErrorKind::Shutdown - }; - - return Err(future::ExecuteError::new(kind, future)); - } - - let _ = self.spawn(future); - Ok(()) - } -} - /// Error returned by the `run` function. #[derive(Debug)] pub struct RunError { @@ -120,14 +66,6 @@ impl Runtime { } } - /// Get a new handle to spawn futures on the single-threaded Tokio runtime - /// - /// Different to the runtime itself, the handle can be sent to different - /// threads. - pub fn handle(&self) -> Handle { - Handle(self.executor.handle().clone()) - } - /// Spawn a future onto the single-threaded Tokio runtime. /// /// See [module level][mod] documentation for more details. diff --git a/actix-rt/src/system.rs b/actix-rt/src/system.rs index d9e093db..63091f73 100644 --- a/actix-rt/src/system.rs +++ b/actix-rt/src/system.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::io; use futures::sync::mpsc::UnboundedSender; @@ -109,7 +110,7 @@ impl System { /// This function will start tokio runtime and will finish once the /// `System::stop()` message get called. /// Function `f` get called within tokio runtime context. - pub fn run(f: F) -> i32 + pub fn run(f: F) -> io::Result<()> where F: FnOnce() + 'static, {