From 5e0fde5dc65b450bedfe343fb11ccd8e11a4e56c Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Wed, 21 Jul 2021 10:43:44 +0900 Subject: [PATCH] Fix clippy warnings and deprecated settings --- rustfmt.toml | 2 +- src/buffer.rs | 6 ++++++ src/protocol/frame/coding.rs | 6 +++--- src/protocol/frame/frame.rs | 1 + src/protocol/message.rs | 24 ++++++++++++++++-------- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index db7f39d..0c9d048 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -4,4 +4,4 @@ # Break complex but short statements a bit less. use_small_heuristics = "Max" -merge_imports = true +imports_granularity = "Crate" diff --git a/src/buffer.rs b/src/buffer.rs index b738594..c080fdb 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -81,6 +81,12 @@ impl Buf for ReadBuffer { } } +impl Default for ReadBuffer { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/protocol/frame/coding.rs b/src/protocol/frame/coding.rs index a37dcd2..827b7ca 100644 --- a/src/protocol/frame/coding.rs +++ b/src/protocol/frame/coding.rs @@ -71,14 +71,14 @@ impl fmt::Display for OpCode { } } -impl Into for OpCode { - fn into(self) -> u8 { +impl From for u8 { + fn from(code: OpCode) -> Self { use self::{ Control::{Close, Ping, Pong}, Data::{Binary, Continue, Text}, OpCode::*, }; - match self { + match code { Data(Continue) => 0, Data(Text) => 1, Data(Binary) => 2, diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index a116489..8222fa1 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -84,6 +84,7 @@ impl FrameHeader { } /// Get the size of the header formatted with given payload length. + #[allow(clippy::len_without_is_empty)] pub fn len(&self, length: u64) -> usize { 2 + LengthFormat::for_length(length).extra_bytes() + if self.mask.is_some() { 4 } else { 0 } } diff --git a/src/protocol/message.rs b/src/protocol/message.rs index 6720c3c..e8eb90e 100644 --- a/src/protocol/message.rs +++ b/src/protocol/message.rs @@ -1,5 +1,5 @@ use std::{ - convert::{AsRef, From, Into}, + convert::{AsRef, From, Into, TryFrom}, fmt, result::Result as StdResult, str, @@ -270,32 +270,40 @@ impl Message { } impl From for Message { - fn from(string: String) -> Message { + fn from(string: String) -> Self { Message::text(string) } } impl<'s> From<&'s str> for Message { - fn from(string: &'s str) -> Message { + fn from(string: &'s str) -> Self { Message::text(string) } } impl<'b> From<&'b [u8]> for Message { - fn from(data: &'b [u8]) -> Message { + fn from(data: &'b [u8]) -> Self { Message::binary(data) } } impl From> for Message { - fn from(data: Vec) -> Message { + fn from(data: Vec) -> Self { Message::binary(data) } } -impl Into> for Message { - fn into(self) -> Vec { - self.into_data() +impl From for Vec { + fn from(message: Message) -> Self { + message.into_data() + } +} + +impl TryFrom for String { + type Error = Error; + + fn try_from(value: Message) -> StdResult { + value.into_text() } }