From a73434bfc4e1156de6bd50e3068a0a6c5772d760 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 16 Feb 2021 19:33:19 +0100 Subject: [PATCH] move code around a bit, use a Cow for string data --- src/protocol/data.rs | 78 +++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/src/protocol/data.rs b/src/protocol/data.rs index 199a949..3c53552 100644 --- a/src/protocol/data.rs +++ b/src/protocol/data.rs @@ -1,4 +1,5 @@ use bytes::Bytes; +use std::borrow::Cow; /// Binary message data #[derive(Debug, Clone)] @@ -72,78 +73,51 @@ impl AsMut<[u8]> for MessageData { } } -/// String message data -#[derive(Debug, Clone)] -pub struct MessageStringData(MessageStringDataImpl); +impl From> for MessageData { + fn from(data: Vec) -> MessageData { + MessageData(MessageDataImpl::Unique(data)) + } +} -/// opaque inner type to allow modifying the implementation in the future -#[derive(Debug, Clone)] -enum MessageStringDataImpl { - Static(&'static str), - Unique(String), +impl From<&'static [u8]> for MessageData { + fn from(data: &'static [u8]) -> MessageData { + MessageData(MessageDataImpl::Shared(Bytes::from_static(data))) + } } -impl PartialEq for MessageStringData { - fn eq(&self, other: &MessageStringData) -> bool { - self.as_ref().eq(other.as_ref()) +impl From for MessageData { + fn from(data: Bytes) -> MessageData { + MessageData(MessageDataImpl::Shared(data)) } } -impl Eq for MessageStringData {} +/// String message data +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct MessageStringData(Cow<'static, str>); + +impl>> From for MessageStringData { + fn from(str: T) -> Self { + MessageStringData(str.into()) + } +} impl From for String { fn from(data: MessageStringData) -> String { - match data.0 { - MessageStringDataImpl::Static(data) => data.into(), - MessageStringDataImpl::Unique(data) => data, - } + data.0.into() } } impl From for MessageData { fn from(data: MessageStringData) -> MessageData { match data.0 { - MessageStringDataImpl::Static(data) => MessageData::from(data.as_bytes()), - MessageStringDataImpl::Unique(data) => MessageData::from(data.into_bytes()), + Cow::Borrowed(data) => MessageData::from(data.as_bytes()), + Cow::Owned(data) => MessageData::from(data.into_bytes()), } } } impl AsRef for MessageStringData { fn as_ref(&self) -> &str { - match &self.0 { - MessageStringDataImpl::Static(data) => *data, - MessageStringDataImpl::Unique(data) => data.as_ref(), - } - } -} - -impl From for MessageStringData { - fn from(string: String) -> MessageStringData { - MessageStringData(MessageStringDataImpl::Unique(string)) - } -} - -impl From<&'static str> for MessageStringData { - fn from(string: &'static str) -> MessageStringData { - MessageStringData(MessageStringDataImpl::Static(string)) - } -} - -impl From> for MessageData { - fn from(data: Vec) -> MessageData { - MessageData(MessageDataImpl::Unique(data)) - } -} - -impl From<&'static [u8]> for MessageData { - fn from(data: &'static [u8]) -> MessageData { - MessageData(MessageDataImpl::Shared(Bytes::from_static(data))) - } -} - -impl From for MessageData { - fn from(data: Bytes) -> MessageData { - MessageData(MessageDataImpl::Shared(data)) + self.0.as_ref() } }