Simplify Buffer::new error type

The returned error is now `Box<Error + Send + Sync>`.
This commit is contained in:
Carl Lerche 2019-03-18 20:26:21 -07:00
parent e05dc90340
commit 733b597f38
No known key found for this signature in database
GPG Key ID: 834DFBC68AEE8D41
3 changed files with 17 additions and 29 deletions

View File

@ -17,8 +17,8 @@ pub struct Closed {
/// Error produced when spawning the worker fails
#[derive(Debug)]
pub struct SpawnError<T> {
inner: T,
pub struct SpawnError {
_p: (),
}
/// Errors produced by `Buffer`.
@ -70,26 +70,16 @@ impl std::error::Error for Closed {}
// ===== impl SpawnError =====
impl<T> SpawnError<T> {
pub(crate) fn new(inner: T) -> SpawnError<T> {
SpawnError { inner }
impl SpawnError {
pub(crate) fn new() -> SpawnError {
SpawnError { _p: () }
}
}
impl<T> fmt::Display for SpawnError<T>
where
T: fmt::Debug,
{
impl fmt::Display for SpawnError {
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>
where
T: std::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.inner)
}
}
impl std::error::Error for SpawnError {}

View File

@ -19,7 +19,7 @@ mod worker;
pub use worker::WorkerExecutor;
use error::{Error, SpawnError};
use error::Error;
use future::ResponseFuture;
use message::Message;
use worker::Worker;
@ -76,7 +76,7 @@ where
{
type Response = S::Response;
type Error = Error;
type LayerError = SpawnError<S>;
type LayerError = Error;
type Service = Buffer<S, Request>;
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
/// 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
T: Send + 'static,
T::Future: Send,
@ -114,16 +114,14 @@ where
///
/// `bound` gives the maximal number of requests that can be queued for the service before
/// 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
E: WorkerExecutor<T, Request>,
{
let (tx, rx) = mpsc::channel(bound);
match Worker::spawn(service, rx, executor) {
Ok(worker) => Ok(Buffer { tx, worker }),
Err(service) => Err(SpawnError::new(service)),
}
Worker::spawn(service, rx, executor)
.map(|worker| Buffer { tx, worker })
}
}

View File

@ -1,4 +1,4 @@
use error::{Closed, Error, ServiceError};
use error::{Closed, SpawnError, Error, ServiceError};
use futures::future::Executor;
use futures::{Async, Future, Poll, Stream};
use message::Message;
@ -56,7 +56,7 @@ where
service: T,
rx: mpsc::Receiver<Message<Request, T::Future>>,
executor: &E,
) -> Result<Handle, T>
) -> Result<Handle, Error>
where
E: WorkerExecutor<T, Request>,
{
@ -75,7 +75,7 @@ where
match executor.execute(worker) {
Ok(()) => Ok(handle),
Err(err) => Err(err.into_future().service),
Err(_) => Err(SpawnError::new().into()),
}
}