From de80b9bced480d407665e46001fc2d7a6874d71e Mon Sep 17 00:00:00 2001 From: "Evgeniy A. Dushistov" Date: Fri, 22 May 2020 01:31:08 +0300 Subject: [PATCH] Change Into to From for CloseCode to u16 conversation As stated in standard library documentation: > One should always prefer implementing From over Into because implementing From automatically > provides one with an implementation of Into thanks to the blanket implementation in the standard library. And this provides the way to use u16::from(close_code) --- src/protocol/frame/coding.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/protocol/frame/coding.rs b/src/protocol/frame/coding.rs index 8380d9f..d3fcdeb 100644 --- a/src/protocol/frame/coding.rs +++ b/src/protocol/frame/coding.rs @@ -202,9 +202,9 @@ impl fmt::Display for CloseCode { } } -impl<'t> Into for &'t CloseCode { - fn into(self) -> u16 { - match *self { +impl From for u16 { + fn from(code: CloseCode) -> u16 { + match code { Normal => 1000, Away => 1001, Protocol => 1002, @@ -227,9 +227,9 @@ impl<'t> Into for &'t CloseCode { } } -impl Into for CloseCode { - fn into(self) -> u16 { - (&self).into() +impl<'t> From<&'t CloseCode> for u16 { + fn from(code: &'t CloseCode) -> u16 { + (*code).into() } } @@ -287,5 +287,6 @@ mod tests { let text = CloseCode::Away; let byte: u16 = text.into(); assert_eq!(byte, 1001u16); + assert_eq!(u16::from(text), 1001u16); } }