diff --git a/src/agent.rs b/src/agent.rs index c2fc1c9..c53876a 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -43,7 +43,7 @@ pub struct AgentBuilder { #[cfg(feature = "cookies")] cookie_store: Option, resolver: ArcResolver, - middleware: Vec>, + middleware: Vec>, } /// Config as built by AgentBuilder and then static for the lifetime of the Agent. @@ -111,7 +111,7 @@ pub(crate) struct AgentState { #[cfg(feature = "cookies")] pub(crate) cookie_tin: CookieTin, pub(crate) resolver: ArcResolver, - pub(crate) middleware: Vec>, + pub(crate) middleware: Vec>, } impl Agent { @@ -601,7 +601,7 @@ impl AgentBuilder { /// /// All requests made by the agent will use this middleware. Middleware is invoked /// in the order they are added to the builder. - pub fn middleware(mut self, m: impl Middleware + Send + Sync + 'static) -> Self { + pub fn middleware(mut self, m: impl Middleware) -> Self { self.middleware.push(Box::new(m)); self } diff --git a/src/middleware.rs b/src/middleware.rs index ff6c2fc..a1b8fe8 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -126,7 +126,7 @@ use crate::{Error, Request, Response}; /// /// # Ok(()) } /// ``` -pub trait Middleware { +pub trait Middleware: Send + Sync + 'static { /// Handle of the middleware logic. fn handle(&self, request: Request, next: MiddlewareNext) -> Result; } @@ -158,7 +158,7 @@ impl<'a> MiddlewareNext<'a> { impl Middleware for F where - F: Fn(Request, MiddlewareNext) -> Result, + F: Fn(Request, MiddlewareNext) -> Result + Send + Sync + 'static, { fn handle(&self, request: Request, next: MiddlewareNext) -> Result { (self)(request, next) diff --git a/src/request.rs b/src/request.rs index fc506f0..42eb455 100644 --- a/src/request.rs +++ b/src/request.rs @@ -7,8 +7,8 @@ use crate::body::Payload; use crate::header::{self, Header}; use crate::middleware::MiddlewareNext; use crate::unit::{self, Unit}; +use crate::Response; use crate::{agent::Agent, error::Error}; -use crate::{Middleware, Response}; pub type Result = std::result::Result; @@ -143,11 +143,7 @@ impl Request { let response = if !self.agent.state.middleware.is_empty() { // Clone agent to get a local copy with same lifetime as Payload let agent = self.agent.clone(); - let chain = &mut agent - .state - .middleware - .iter() - .map(|mw| mw.as_ref() as &dyn Middleware); + let chain = &mut agent.state.middleware.iter().map(|mw| mw.as_ref()); let request_fn = Box::new(request_fn);