Add Display to CloseFrame and CloseCode

Signed-off-by: Alexey Galakhov <agalakhov@snapview.de>
pull/12/head
Alexey Galakhov 8 years ago
parent bd37c71609
commit 8df6bdbeb0
  1. 17
      src/protocol/frame/coding.rs
  2. 16
      src/protocol/frame/frame.rs

@ -193,9 +193,16 @@ impl CloseCode {
}
}
impl Into<u16> for CloseCode {
impl fmt::Display for CloseCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let code: u16 = self.into();
write!(f, "{}", code)
}
}
impl<'t> Into<u16> for &'t CloseCode {
fn into(self) -> u16 {
match self {
match *self {
Normal => 1000,
Away => 1001,
Protocol => 1002,
@ -218,6 +225,12 @@ impl Into<u16> for CloseCode {
}
}
impl Into<u16> for CloseCode {
fn into(self) -> u16 {
(&self).into()
}
}
impl From<u16> for CloseCode {
fn from(code: u16) -> CloseCode {
match code {

@ -35,6 +35,22 @@ pub struct CloseFrame<'t> {
pub reason: Cow<'t, str>,
}
impl<'t> CloseFrame<'t> {
/// Convert into a owned string.
pub fn into_owned(self) -> CloseFrame<'static> {
CloseFrame {
code: self.code,
reason: self.reason.into_owned().into(),
}
}
}
impl<'t> fmt::Display for CloseFrame<'t> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ({})", self.reason, self.code)
}
}
/// A struct representing a WebSocket frame.
#[derive(Debug, Clone)]
pub struct Frame {

Loading…
Cancel
Save