Simplify Buffer::new error type
The returned error is now `Box<Error + Send + Sync>`.
This commit is contained in:
parent
e05dc90340
commit
733b597f38
|
@ -17,8 +17,8 @@ pub struct Closed {
|
||||||
|
|
||||||
/// Error produced when spawning the worker fails
|
/// Error produced when spawning the worker fails
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SpawnError<T> {
|
pub struct SpawnError {
|
||||||
inner: T,
|
_p: (),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Errors produced by `Buffer`.
|
/// Errors produced by `Buffer`.
|
||||||
|
@ -70,26 +70,16 @@ impl std::error::Error for Closed {}
|
||||||
|
|
||||||
// ===== impl SpawnError =====
|
// ===== impl SpawnError =====
|
||||||
|
|
||||||
impl<T> SpawnError<T> {
|
impl SpawnError {
|
||||||
pub(crate) fn new(inner: T) -> SpawnError<T> {
|
pub(crate) fn new() -> SpawnError {
|
||||||
SpawnError { inner }
|
SpawnError { _p: () }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> fmt::Display for SpawnError<T>
|
impl fmt::Display for SpawnError {
|
||||||
where
|
|
||||||
T: fmt::Debug,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "error spawning buffer task: {:?}", self.inner)
|
write!(f, "failed to spawn Buffer worker task")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> std::error::Error for SpawnError<T>
|
impl std::error::Error for SpawnError {}
|
||||||
where
|
|
||||||
T: std::error::Error + 'static,
|
|
||||||
{
|
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
||||||
Some(&self.inner)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ mod worker;
|
||||||
|
|
||||||
pub use worker::WorkerExecutor;
|
pub use worker::WorkerExecutor;
|
||||||
|
|
||||||
use error::{Error, SpawnError};
|
use error::Error;
|
||||||
use future::ResponseFuture;
|
use future::ResponseFuture;
|
||||||
use message::Message;
|
use message::Message;
|
||||||
use worker::Worker;
|
use worker::Worker;
|
||||||
|
@ -76,7 +76,7 @@ where
|
||||||
{
|
{
|
||||||
type Response = S::Response;
|
type Response = S::Response;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type LayerError = SpawnError<S>;
|
type LayerError = Error;
|
||||||
type Service = Buffer<S, Request>;
|
type Service = Buffer<S, Request>;
|
||||||
|
|
||||||
fn layer(&self, service: S) -> Result<Self::Service, Self::LayerError> {
|
fn layer(&self, service: S) -> Result<Self::Service, Self::LayerError> {
|
||||||
|
@ -96,7 +96,7 @@ where
|
||||||
///
|
///
|
||||||
/// The default Tokio executor is used to run the given service, which means that this method
|
/// The default Tokio executor is used to run the given service, which means that this method
|
||||||
/// must be called while on the Tokio runtime.
|
/// must be called while on the Tokio runtime.
|
||||||
pub fn new(service: T, bound: usize) -> Result<Self, SpawnError<T>>
|
pub fn new(service: T, bound: usize) -> Result<Self, Error>
|
||||||
where
|
where
|
||||||
T: Send + 'static,
|
T: Send + 'static,
|
||||||
T::Future: Send,
|
T::Future: Send,
|
||||||
|
@ -114,16 +114,14 @@ where
|
||||||
///
|
///
|
||||||
/// `bound` gives the maximal number of requests that can be queued for the service before
|
/// `bound` gives the maximal number of requests that can be queued for the service before
|
||||||
/// backpressure is applied to callers.
|
/// backpressure is applied to callers.
|
||||||
pub fn with_executor<E>(service: T, bound: usize, executor: &E) -> Result<Self, SpawnError<T>>
|
pub fn with_executor<E>(service: T, bound: usize, executor: &E) -> Result<Self, Error>
|
||||||
where
|
where
|
||||||
E: WorkerExecutor<T, Request>,
|
E: WorkerExecutor<T, Request>,
|
||||||
{
|
{
|
||||||
let (tx, rx) = mpsc::channel(bound);
|
let (tx, rx) = mpsc::channel(bound);
|
||||||
|
|
||||||
match Worker::spawn(service, rx, executor) {
|
Worker::spawn(service, rx, executor)
|
||||||
Ok(worker) => Ok(Buffer { tx, worker }),
|
.map(|worker| Buffer { tx, worker })
|
||||||
Err(service) => Err(SpawnError::new(service)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use error::{Closed, Error, ServiceError};
|
use error::{Closed, SpawnError, Error, ServiceError};
|
||||||
use futures::future::Executor;
|
use futures::future::Executor;
|
||||||
use futures::{Async, Future, Poll, Stream};
|
use futures::{Async, Future, Poll, Stream};
|
||||||
use message::Message;
|
use message::Message;
|
||||||
|
@ -56,7 +56,7 @@ where
|
||||||
service: T,
|
service: T,
|
||||||
rx: mpsc::Receiver<Message<Request, T::Future>>,
|
rx: mpsc::Receiver<Message<Request, T::Future>>,
|
||||||
executor: &E,
|
executor: &E,
|
||||||
) -> Result<Handle, T>
|
) -> Result<Handle, Error>
|
||||||
where
|
where
|
||||||
E: WorkerExecutor<T, Request>,
|
E: WorkerExecutor<T, Request>,
|
||||||
{
|
{
|
||||||
|
@ -75,7 +75,7 @@ where
|
||||||
|
|
||||||
match executor.execute(worker) {
|
match executor.execute(worker) {
|
||||||
Ok(()) => Ok(handle),
|
Ok(()) => Ok(handle),
|
||||||
Err(err) => Err(err.into_future().service),
|
Err(_) => Err(SpawnError::new().into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue