diff --git a/src/context.rs b/src/context.rs index e56c17378..601cfe585 100644 --- a/src/context.rs +++ b/src/context.rs @@ -104,24 +104,29 @@ where #[inline] /// Create a new HTTP Context from a request and an actor pub fn new(req: HttpRequest, actor: A) -> HttpContext { - HttpContext::from_request(req).actor(actor) - } - - /// Create a new HTTP Context from a request - pub fn from_request(req: HttpRequest) -> HttpContext { HttpContext { - inner: ContextImpl::new(None), + inner: ContextImpl::new(Some(actor)), stream: None, request: req, disconnected: false, } } - #[inline] - /// Set the actor of this context - pub fn actor(mut self, actor: A) -> HttpContext { - self.inner.set_actor(actor); - self + /// Create a new HTTP Context + pub fn with_factory(req: HttpRequest, f: F) -> HttpContext + where + F: FnOnce(&mut Self) -> A + 'static, + { + let mut ctx = HttpContext { + inner: ContextImpl::new(None), + stream: None, + request: req, + disconnected: false, + }; + + let act = f(&mut ctx); + ctx.inner.set_actor(act); + ctx } } diff --git a/src/ws/context.rs b/src/ws/context.rs index 9237eab4f..34346f1ee 100644 --- a/src/ws/context.rs +++ b/src/ws/context.rs @@ -88,24 +88,29 @@ where #[inline] /// Create a new Websocket context from a request and an actor pub fn new(req: HttpRequest, actor: A) -> WebsocketContext { - WebsocketContext::from_request(req).actor(actor) - } - - /// Create a new Websocket context from a request - pub fn from_request(req: HttpRequest) -> WebsocketContext { WebsocketContext { - inner: ContextImpl::new(None), + inner: ContextImpl::new(Some(actor)), stream: None, request: req, disconnected: false, } } - #[inline] - /// Set actor for this execution context - pub fn actor(mut self, actor: A) -> WebsocketContext { - self.inner.set_actor(actor); - self + /// Create a new Websocket context + pub fn with_factory(req: HttpRequest, f: F) -> Self + where + F: FnOnce(&mut Self) -> A + 'static, + { + let mut ctx = WebsocketContext { + inner: ContextImpl::new(None), + stream: None, + request: req, + disconnected: false, + }; + + let act = f(&mut ctx); + ctx.inner.set_actor(act); + ctx } }