mirror of
https://github.com/fafhrd91/actix-net
synced 2025-02-06 22:35:54 +01:00
return io::Result from run method, remove Handle
This commit is contained in:
parent
25f1eae51f
commit
c03d869694
@ -1,5 +1,11 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.0] - 2019-03-06
|
||||||
|
|
||||||
|
* `run` method returns `io::Result<()>`
|
||||||
|
|
||||||
|
* Removed `Handle`
|
||||||
|
|
||||||
## [0.1.0] - 2018-12-09
|
## [0.1.0] - 2018-12-09
|
||||||
|
|
||||||
* Initial release
|
* Initial release
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-rt"
|
name = "actix-rt"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix runtime"
|
description = "Actix runtime"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -20,7 +20,7 @@ path = "src/lib.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1.24"
|
futures = "0.1.25"
|
||||||
tokio-current-thread = "0.1"
|
tokio-current-thread = "0.1"
|
||||||
tokio-executor = "0.1.5"
|
tokio-executor = "0.1.5"
|
||||||
tokio-reactor = "0.1.7"
|
tokio-reactor = "0.1.7"
|
||||||
|
@ -72,7 +72,7 @@ impl Builder {
|
|||||||
/// This function will start tokio runtime and will finish once the
|
/// This function will start tokio runtime and will finish once the
|
||||||
/// `System::stop()` message get called.
|
/// `System::stop()` message get called.
|
||||||
/// Function `f` get called within tokio runtime context.
|
/// Function `f` get called within tokio runtime context.
|
||||||
pub fn run<F>(self, f: F) -> i32
|
pub fn run<F>(self, f: F) -> io::Result<()>
|
||||||
where
|
where
|
||||||
F: FnOnce() + 'static,
|
F: FnOnce() + 'static,
|
||||||
{
|
{
|
||||||
@ -140,7 +140,7 @@ pub struct SystemRunner {
|
|||||||
impl SystemRunner {
|
impl SystemRunner {
|
||||||
/// This function will start event loop and will finish once the
|
/// This function will start event loop and will finish once the
|
||||||
/// `System::stop()` function is called.
|
/// `System::stop()` function is called.
|
||||||
pub fn run(self) -> i32 {
|
pub fn run(self) -> io::Result<()> {
|
||||||
let SystemRunner { mut rt, stop, .. } = self;
|
let SystemRunner { mut rt, stop, .. } = self;
|
||||||
|
|
||||||
// run loop
|
// run loop
|
||||||
@ -148,12 +148,21 @@ impl SystemRunner {
|
|||||||
Arbiter::run_system();
|
Arbiter::run_system();
|
||||||
Ok::<_, ()>(())
|
Ok::<_, ()>(())
|
||||||
}));
|
}));
|
||||||
let code = match rt.block_on(stop) {
|
let result = match rt.block_on(stop) {
|
||||||
Ok(code) => code,
|
Ok(code) => {
|
||||||
Err(_) => 1,
|
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();
|
Arbiter::stop_system();
|
||||||
code
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a future and wait for result.
|
/// Execute a future and wait for result.
|
||||||
|
@ -7,7 +7,7 @@ mod system;
|
|||||||
|
|
||||||
pub use self::arbiter::Arbiter;
|
pub use self::arbiter::Arbiter;
|
||||||
pub use self::builder::{Builder, SystemRunner};
|
pub use self::builder::{Builder, SystemRunner};
|
||||||
pub use self::runtime::{Handle, Runtime};
|
pub use self::runtime::Runtime;
|
||||||
pub use self::system::System;
|
pub use self::system::System;
|
||||||
|
|
||||||
/// Spawns a future on the current arbiter.
|
/// Spawns a future on the current arbiter.
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::{fmt, io};
|
||||||
use std::io;
|
|
||||||
|
|
||||||
use futures::{future, Future};
|
use futures::Future;
|
||||||
use tokio_current_thread::Handle as ExecutorHandle;
|
|
||||||
use tokio_current_thread::{self as current_thread, CurrentThread};
|
use tokio_current_thread::{self as current_thread, CurrentThread};
|
||||||
use tokio_executor;
|
use tokio_executor;
|
||||||
use tokio_reactor::{self, Reactor};
|
use tokio_reactor::{self, Reactor};
|
||||||
@ -26,58 +24,6 @@ pub struct Runtime {
|
|||||||
executor: CurrentThread<Timer<Reactor>>,
|
executor: CurrentThread<Timer<Reactor>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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<F>(&self, future: F) -> Result<(), tokio_executor::SpawnError>
|
|
||||||
where
|
|
||||||
F: Future<Item = (), Error = ()> + 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<T> future::Executor<T> for Handle
|
|
||||||
where
|
|
||||||
T: Future<Item = (), Error = ()> + Send + 'static,
|
|
||||||
{
|
|
||||||
fn execute(&self, future: T) -> Result<(), future::ExecuteError<T>> {
|
|
||||||
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.
|
/// Error returned by the `run` function.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RunError {
|
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.
|
/// Spawn a future onto the single-threaded Tokio runtime.
|
||||||
///
|
///
|
||||||
/// See [module level][mod] documentation for more details.
|
/// See [module level][mod] documentation for more details.
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
use futures::sync::mpsc::UnboundedSender;
|
use futures::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
@ -109,7 +110,7 @@ impl System {
|
|||||||
/// This function will start tokio runtime and will finish once the
|
/// This function will start tokio runtime and will finish once the
|
||||||
/// `System::stop()` message get called.
|
/// `System::stop()` message get called.
|
||||||
/// Function `f` get called within tokio runtime context.
|
/// Function `f` get called within tokio runtime context.
|
||||||
pub fn run<F>(f: F) -> i32
|
pub fn run<F>(f: F) -> io::Result<()>
|
||||||
where
|
where
|
||||||
F: FnOnce() + 'static,
|
F: FnOnce() + 'static,
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user