From 6211e60319dba4fa9e17b0aad1a46de7b0cb6591 Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Thu, 14 Jul 2022 09:26:10 +0100 Subject: [PATCH] Add InlineStringError to strings module, rename ShString repr variant --- src/strings/fixed.rs | 26 ++++++++++++-------------- src/strings/inline.rs | 20 ++++++++++---------- src/strings/mod.rs | 2 +- src/strings/shstring.rs | 16 ++++++++-------- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/strings/fixed.rs b/src/strings/fixed.rs index 33ab3fc..67f5764 100644 --- a/src/strings/fixed.rs +++ b/src/strings/fixed.rs @@ -26,9 +26,9 @@ impl FixedString { pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result { match bytes.try_into() { Ok(bytes) => Ok(Self::from_raw_array(bytes)), - Err(_) => Err(Error::BadLength { - expected: N, - actual: bytes.len(), + Err(_) => Err(Error { + expected_len: N, + actual_len: bytes.len(), }), } } @@ -175,21 +175,19 @@ impl fmt::Display for FixedString { } #[derive(Debug)] -pub enum Error { - BadLength { expected: usize, actual: usize }, +pub struct Error { + expected_len: usize, + actual_len: usize, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::BadLength { expected, actual } => { - write!( - f, - "expected {} bytes of string data, found {} bytes", - expected, actual - ) - } - } + write!( + f, + "expected {} bytes of string data, found {} bytes", + self.expected_len, + self.actual_len + ) } } diff --git a/src/strings/inline.rs b/src/strings/inline.rs index 7f6d37f..eb04079 100644 --- a/src/strings/inline.rs +++ b/src/strings/inline.rs @@ -46,7 +46,7 @@ impl InlineString { } #[inline] - pub fn new(s: &S) -> Result + pub fn new(s: &S) -> Result where S: AsRef + ?Sized, { @@ -59,7 +59,7 @@ impl InlineString { let len = u8::try_from(s.len()) .ok() .and_then(|len| (len <= Self::MAX_LEN).then_some(len)) - .ok_or(InlineStringError { + .ok_or(Error { max_len: N, actual_len: s.len(), })?; @@ -162,7 +162,7 @@ impl borrow::BorrowMut for InlineString { } impl<'a, const N: usize> TryFrom<&'a str> for InlineString { - type Error = InlineStringError; + type Error = Error; #[inline] fn try_from(s: &'a str) -> Result { @@ -171,7 +171,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for InlineString { } impl TryFrom for InlineString { - type Error = InlineStringError; + type Error = Error; #[inline] fn try_from(s: String) -> Result { @@ -180,7 +180,7 @@ impl TryFrom for InlineString { } impl<'a, const N: usize> TryFrom> for InlineString { - type Error = InlineStringError; + type Error = Error; #[inline] fn try_from(s: Cow<'a, str>) -> Result { @@ -226,7 +226,7 @@ impl Hash for InlineString { } impl str::FromStr for InlineString { - type Err = InlineStringError; + type Err = Error; #[inline] fn from_str(s: &str) -> Result { @@ -249,12 +249,12 @@ impl fmt::Display for InlineString { } #[derive(Debug)] -pub struct InlineStringError { +pub struct Error { max_len: usize, actual_len: usize, } -impl InlineStringError { +impl Error { pub fn max_len(&self) -> usize { self.max_len } @@ -264,7 +264,7 @@ impl InlineStringError { } } -impl fmt::Display for InlineStringError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -275,4 +275,4 @@ impl fmt::Display for InlineStringError { } } -impl error::Error for InlineStringError {} +impl error::Error for Error {} diff --git a/src/strings/mod.rs b/src/strings/mod.rs index dcf3a51..8a3723a 100644 --- a/src/strings/mod.rs +++ b/src/strings/mod.rs @@ -4,5 +4,5 @@ pub mod inline; pub mod shstring; pub use fixed::{FixedString, Error as FixedStringError}; -pub use inline::InlineString; +pub use inline::{InlineString, Error as InlineStringError}; pub use shstring::{ShString, ShString22}; diff --git a/src/strings/shstring.rs b/src/strings/shstring.rs index d11bf1d..d087555 100644 --- a/src/strings/shstring.rs +++ b/src/strings/shstring.rs @@ -49,7 +49,7 @@ impl ShString { { match InlineString::new(&s) { Ok(stack_buf) => Self(Repr::Inline(stack_buf)), - Err(_) => Self(Repr::Heap(Box::::from(s))), + Err(_) => Self(Repr::Boxed(Box::::from(s))), } } @@ -59,7 +59,7 @@ impl ShString { pub fn as_str(&self) -> &str { match self { Self(Repr::Inline(buf)) => buf, - Self(Repr::Heap(buf)) => buf, + Self(Repr::Boxed(buf)) => buf, } } @@ -69,7 +69,7 @@ impl ShString { pub fn as_str_mut(&mut self) -> &mut str { match self { Self(Repr::Inline(buf)) => buf, - Self(Repr::Heap(buf)) => buf, + Self(Repr::Boxed(buf)) => buf, } } @@ -79,7 +79,7 @@ impl ShString { pub fn into_string(self) -> String { match self { Self(Repr::Inline(buf)) => buf.into_string(), - Self(Repr::Heap(buf)) => buf.into_string(), + Self(Repr::Boxed(buf)) => buf.into_string(), } } @@ -95,7 +95,7 @@ impl ShString { pub fn len(&self) -> usize { match self { Self(Repr::Inline(buf)) => buf.len(), - Self(Repr::Heap(buf)) => buf.len(), + Self(Repr::Boxed(buf)) => buf.len(), } } @@ -114,7 +114,7 @@ impl ShString { pub fn is_empty(&self) -> bool { match self { Self(Repr::Inline(buf)) => buf.is_empty(), - Self(Repr::Heap(buf)) => buf.is_empty(), + Self(Repr::Boxed(buf)) => buf.is_empty(), } } @@ -133,7 +133,7 @@ impl ShString { pub fn heap_allocated(&self) -> bool { match self { Self(Repr::Inline(_)) => false, - Self(Repr::Heap(_)) => true, + Self(Repr::Boxed(_)) => true, } } } @@ -294,7 +294,7 @@ impl<'de, const N: usize> serde::Deserialize<'de> for ShString { #[derive(Clone)] enum Repr { Inline(InlineString), - Heap(Box), + Boxed(Box), } #[cfg(test)]