From 33050f55a3fa53bc1e51f1996c5ef53e71c331a1 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 17 Jun 2018 03:10:44 +0600 Subject: [PATCH] remove Context::actor() method --- src/context.rs | 27 ++++++++++++++++----------- src/ws/context.rs | 27 ++++++++++++++++----------- 2 files changed, 32 insertions(+), 22 deletions(-) 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 } }