From be12d5e6fc8e3474e65f25a50f88d4668ad754b1 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 9 May 2018 05:48:06 -0700 Subject: [PATCH] make WsWriter trait optional --- CHANGES.md | 5 +++ src/lib.rs | 2 ++ src/ws/client.rs | 34 ++++++++++++++++-- src/ws/context.rs | 90 +++++++++++++++++++++++++++++++---------------- src/ws/mod.rs | 10 +++--- 5 files changed, 104 insertions(+), 37 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a2d3ae25..51267c76 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Changes +## 0.6.2 (2018-05-09) + +* WsWriter trait is optional. + + ## 0.6.1 (2018-05-08) * Fix http/2 payload streaming #215 diff --git a/src/lib.rs b/src/lib.rs index 92a319bc..62d8fd83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -175,6 +175,8 @@ pub use httprequest::HttpRequest; pub use httpresponse::HttpResponse; pub use json::Json; pub use scope::Scope; + +#[doc(hidden)] pub use ws::WsWriter; #[cfg(feature = "openssl")] diff --git a/src/ws/client.rs b/src/ws/client.rs index aa392a90..780de03c 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -518,9 +518,7 @@ impl ClientWriter { fn as_mut(&mut self) -> &mut Inner { unsafe { &mut *self.inner.get() } } -} -impl WsWriter for ClientWriter { /// Send text frame #[inline] fn text>(&mut self, text: T) { @@ -561,3 +559,35 @@ impl WsWriter for ClientWriter { self.write(Frame::close(reason, true)); } } + +impl WsWriter for ClientWriter { + /// Send text frame + #[inline] + fn send_text>(&mut self, text: T) { + self.text(text) + } + + /// Send binary frame + #[inline] + fn send_binary>(&mut self, data: B) { + self.binary(data) + } + + /// Send ping frame + #[inline] + fn send_ping(&mut self, message: &str) { + self.ping(message) + } + + /// Send pong frame + #[inline] + fn send_pong(&mut self, message: &str) { + self.pong(message) + } + + /// Send close frame + #[inline] + fn send_close(&mut self, reason: Option) { + self.close(reason); + } +} diff --git a/src/ws/context.rs b/src/ws/context.rs index 6114a030..36f73e99 100644 --- a/src/ws/context.rs +++ b/src/ws/context.rs @@ -149,36 +149,6 @@ where Drain::new(rx) } - /// Check if connection still open - #[inline] - pub fn connected(&self) -> bool { - !self.disconnected - } - - #[inline] - fn add_frame(&mut self, frame: ContextFrame) { - if self.stream.is_none() { - self.stream = Some(SmallVec::new()); - } - if let Some(s) = self.stream.as_mut() { - s.push(frame) - } - self.inner.modify(); - } - - /// Handle of the running future - /// - /// SpawnHandle is the handle returned by `AsyncContext::spawn()` method. - pub fn handle(&self) -> SpawnHandle { - self.inner.curr_handle() - } -} - -impl WsWriter for WebsocketContext -where - A: Actor, - S: 'static, -{ /// Send text frame #[inline] fn text>(&mut self, text: T) { @@ -218,6 +188,66 @@ where fn close(&mut self, reason: Option) { self.write(Frame::close(reason, false)); } + + /// Check if connection still open + #[inline] + pub fn connected(&self) -> bool { + !self.disconnected + } + + #[inline] + fn add_frame(&mut self, frame: ContextFrame) { + if self.stream.is_none() { + self.stream = Some(SmallVec::new()); + } + if let Some(s) = self.stream.as_mut() { + s.push(frame) + } + self.inner.modify(); + } + + /// Handle of the running future + /// + /// SpawnHandle is the handle returned by `AsyncContext::spawn()` method. + pub fn handle(&self) -> SpawnHandle { + self.inner.curr_handle() + } +} + +impl WsWriter for WebsocketContext +where + A: Actor, + S: 'static, +{ + /// Send text frame + #[inline] + fn send_text>(&mut self, text: T) { + self.text(text) + } + + /// Send binary frame + #[inline] + fn send_binary>(&mut self, data: B) { + self.binary(data) + } + + /// Send ping frame + #[inline] + fn send_ping(&mut self, message: &str) { + self.ping(message) + } + + /// Send pong frame + #[inline] + fn send_pong(&mut self, message: &str) { + self.pong(message) + } + + /// Send close frame + #[inline] + fn send_close(&mut self, reason: Option) { + self.close(reason) + } } impl ActorHttpContext for WebsocketContext diff --git a/src/ws/mod.rs b/src/ws/mod.rs index a39b95b1..9fb40dd9 100644 --- a/src/ws/mod.rs +++ b/src/ws/mod.rs @@ -343,15 +343,15 @@ where /// Common writing methods for a websocket. pub trait WsWriter { /// Send a text - fn text>(&mut self, text: T); + fn send_text>(&mut self, text: T); /// Send a binary - fn binary>(&mut self, data: B); + fn send_binary>(&mut self, data: B); /// Send a ping message - fn ping(&mut self, message: &str); + fn send_ping(&mut self, message: &str); /// Send a pong message - fn pong(&mut self, message: &str); + fn send_pong(&mut self, message: &str); /// Close the connection - fn close(&mut self, reason: Option); + fn send_close(&mut self, reason: Option); } #[cfg(test)]