From 74e2bbf40621e1b525b44f64c038dc732b68f70c Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Thu, 14 Jul 2022 09:21:01 +0100 Subject: [PATCH] Rename StackString to InlineString to reflect the fact that it may not always be on the stack, rename modules --- src/strings/{fixed_string.rs => fixed.rs} | 0 src/strings/{stack_string.rs => inline.rs} | 84 +++++++++++----------- src/strings/mod.rs | 8 +-- src/strings/shstring.rs | 22 +++--- 4 files changed, 57 insertions(+), 57 deletions(-) rename src/strings/{fixed_string.rs => fixed.rs} (100%) rename src/strings/{stack_string.rs => inline.rs} (70%) diff --git a/src/strings/fixed_string.rs b/src/strings/fixed.rs similarity index 100% rename from src/strings/fixed_string.rs rename to src/strings/fixed.rs diff --git a/src/strings/stack_string.rs b/src/strings/inline.rs similarity index 70% rename from src/strings/stack_string.rs rename to src/strings/inline.rs index 9ab08eb..7f6d37f 100644 --- a/src/strings/stack_string.rs +++ b/src/strings/inline.rs @@ -9,12 +9,12 @@ use std::{ }; #[derive(Clone)] -pub struct StackString { +pub struct InlineString { buf: [u8; N], len: u8, } -impl StackString { +impl InlineString { const MAX_LEN: u8 = { #[allow(clippy::cast_possible_truncation, clippy::checked_conversions)] if N <= u8::MAX as usize { @@ -24,7 +24,7 @@ impl StackString { } }; - /// Creates a new `StackString` from a given byte buffer and length. + /// Creates a new `InlineString` from a given byte buffer and length. /// /// # Safety /// @@ -35,7 +35,7 @@ impl StackString { Self { buf, len } } - /// Returns a new empty `StackString`. + /// Returns a new empty `InlineString`. #[inline] #[must_use] pub const fn empty() -> Self { @@ -46,7 +46,7 @@ impl StackString { } #[inline] - pub fn new(s: &S) -> Result + pub fn new(s: &S) -> Result where S: AsRef + ?Sized, { @@ -55,11 +55,11 @@ impl StackString { let s = >::as_ref(s).as_bytes(); // If the length of the string is greater than `Self::MAX_LEN`, it will not fit in the - // stack buffer so return `None`. + // buffer so return `None`. let len = u8::try_from(s.len()) .ok() .and_then(|len| (len <= Self::MAX_LEN).then_some(len)) - .ok_or(StackStringError { + .ok_or(InlineStringError { max_len: N, actual_len: s.len(), })?; @@ -76,24 +76,24 @@ impl StackString { /// Returns a string slice pointing to the underlying string data. pub fn as_str(&self) -> &str { // SAFETY: - // `len` being less than or equal to `N` is an invariant of `StackString`, so it is + // `len` being less than or equal to `N` is an invariant of `InlineString`, so it is // always within the bounds of `buf`. let slice = unsafe { self.buf.get_unchecked(..usize::from(self.len)) }; // SAFETY: - // The first `len` bytes of `buf` being valid UTF-8 is an invariant of `StackString`. + // The first `len` bytes of `buf` being valid UTF-8 is an invariant of `InlineString`. unsafe { str::from_utf8_unchecked(slice) } } /// Returns a mutable string slice pointing to the underlying string data. pub fn as_str_mut(&mut self) -> &mut str { // SAFETY: - // `len` being less than or equal to `N` is an invariant of `StackString`, so it is + // `len` being less than or equal to `N` is an invariant of `InlineString`, so it is // always within the bounds of `buf`. let slice = unsafe { self.buf.get_unchecked_mut(..usize::from(self.len)) }; // SAFETY: - // The first `len` bytes of `buf` being valid UTF-8 is an invariant of `StackString`. + // The first `len` bytes of `buf` being valid UTF-8 is an invariant of `InlineString`. unsafe { str::from_utf8_unchecked_mut(slice) } } @@ -110,14 +110,14 @@ impl StackString { } } -impl Default for StackString { +impl Default for InlineString { #[inline] fn default() -> Self { Self::empty() } } -impl ops::Deref for StackString { +impl ops::Deref for InlineString { type Target = str; #[inline] @@ -126,43 +126,43 @@ impl ops::Deref for StackString { } } -impl ops::DerefMut for StackString { +impl ops::DerefMut for InlineString { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { self.as_str_mut() } } -impl AsRef for StackString { +impl AsRef for InlineString { #[inline] fn as_ref(&self) -> &str { self } } -impl AsMut for StackString { +impl AsMut for InlineString { #[inline] fn as_mut(&mut self) -> &mut str { self } } -impl borrow::Borrow for StackString { +impl borrow::Borrow for InlineString { #[inline] fn borrow(&self) -> &str { self } } -impl borrow::BorrowMut for StackString { +impl borrow::BorrowMut for InlineString { #[inline] fn borrow_mut(&mut self) -> &mut str { self } } -impl<'a, const N: usize> TryFrom<&'a str> for StackString { - type Error = StackStringError; +impl<'a, const N: usize> TryFrom<&'a str> for InlineString { + type Error = InlineStringError; #[inline] fn try_from(s: &'a str) -> Result { @@ -170,8 +170,8 @@ impl<'a, const N: usize> TryFrom<&'a str> for StackString { } } -impl TryFrom for StackString { - type Error = StackStringError; +impl TryFrom for InlineString { + type Error = InlineStringError; #[inline] fn try_from(s: String) -> Result { @@ -179,8 +179,8 @@ impl TryFrom for StackString { } } -impl<'a, const N: usize> TryFrom> for StackString { - type Error = StackStringError; +impl<'a, const N: usize> TryFrom> for InlineString { + type Error = InlineStringError; #[inline] fn try_from(s: Cow<'a, str>) -> Result { @@ -188,45 +188,45 @@ impl<'a, const N: usize> TryFrom> for StackString { } } -impl From> for String { +impl From> for String { #[inline] - fn from(s: StackString) -> Self { + fn from(s: InlineString) -> Self { s.into_string() } } -impl PartialEq> for StackString { +impl PartialEq> for InlineString { #[inline] - fn eq(&self, other: &StackString) -> bool { + fn eq(&self, other: &InlineString) -> bool { **self == **other } } -impl Eq for StackString {} +impl Eq for InlineString {} -impl PartialOrd> for StackString { +impl PartialOrd> for InlineString { #[inline] - fn partial_cmp(&self, other: &StackString) -> Option { + fn partial_cmp(&self, other: &InlineString) -> Option { (**self).partial_cmp(&**other) } } -impl Ord for StackString { +impl Ord for InlineString { #[inline] fn cmp(&self, other: &Self) -> Ordering { (**self).cmp(&**other) } } -impl Hash for StackString { +impl Hash for InlineString { #[inline] fn hash(&self, state: &mut H) { (**self).hash(state); } } -impl str::FromStr for StackString { - type Err = StackStringError; +impl str::FromStr for InlineString { + type Err = InlineStringError; #[inline] fn from_str(s: &str) -> Result { @@ -234,14 +234,14 @@ impl str::FromStr for StackString { } } -impl fmt::Debug for StackString { +impl fmt::Debug for InlineString { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl fmt::Display for StackString { +impl fmt::Display for InlineString { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&**self, f) @@ -249,12 +249,12 @@ impl fmt::Display for StackString { } #[derive(Debug)] -pub struct StackStringError { +pub struct InlineStringError { max_len: usize, actual_len: usize, } -impl StackStringError { +impl InlineStringError { pub fn max_len(&self) -> usize { self.max_len } @@ -264,15 +264,15 @@ impl StackStringError { } } -impl fmt::Display for StackStringError { +impl fmt::Display for InlineStringError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "string of length {} exceeds limit for `StackString<{}>`", + "string of length {} exceeds limit for `InlineString<{}>`", self.actual_len, self.max_len ) } } -impl error::Error for StackStringError {} +impl error::Error for InlineStringError {} diff --git a/src/strings/mod.rs b/src/strings/mod.rs index 9016eb7..dcf3a51 100644 --- a/src/strings/mod.rs +++ b/src/strings/mod.rs @@ -1,8 +1,8 @@ pub mod experimental; -pub mod fixed_string; -pub mod stack_string; +pub mod fixed; +pub mod inline; pub mod shstring; -pub use fixed_string::{FixedString, Error as FixedStringError}; -pub use stack_string::StackString; +pub use fixed::{FixedString, Error as FixedStringError}; +pub use inline::InlineString; pub use shstring::{ShString, ShString22}; diff --git a/src/strings/shstring.rs b/src/strings/shstring.rs index 7d3097e..d11bf1d 100644 --- a/src/strings/shstring.rs +++ b/src/strings/shstring.rs @@ -8,7 +8,7 @@ use std::{ str::FromStr, }; -use super::StackString; +use super::InlineString; /// A non-growable string where strings 22 bytes or shorter are stored on the stack and longer /// strings are stored on the heap. @@ -35,7 +35,7 @@ impl ShString { #[inline] #[must_use] pub const fn empty() -> Self { - Self(Repr::Stack(StackString::empty())) + Self(Repr::Inline(InlineString::empty())) } /// Creates a new `ShString` from the given string slice, putting it on the stack if possible @@ -47,8 +47,8 @@ impl ShString { S: AsRef, Box: From, { - match StackString::new(&s) { - Ok(stack_buf) => Self(Repr::Stack(stack_buf)), + match InlineString::new(&s) { + Ok(stack_buf) => Self(Repr::Inline(stack_buf)), Err(_) => Self(Repr::Heap(Box::::from(s))), } } @@ -58,7 +58,7 @@ impl ShString { #[must_use] pub fn as_str(&self) -> &str { match self { - Self(Repr::Stack(buf)) => buf, + Self(Repr::Inline(buf)) => buf, Self(Repr::Heap(buf)) => buf, } } @@ -68,7 +68,7 @@ impl ShString { #[must_use] pub fn as_str_mut(&mut self) -> &mut str { match self { - Self(Repr::Stack(buf)) => buf, + Self(Repr::Inline(buf)) => buf, Self(Repr::Heap(buf)) => buf, } } @@ -78,7 +78,7 @@ impl ShString { #[must_use] pub fn into_string(self) -> String { match self { - Self(Repr::Stack(buf)) => buf.into_string(), + Self(Repr::Inline(buf)) => buf.into_string(), Self(Repr::Heap(buf)) => buf.into_string(), } } @@ -94,7 +94,7 @@ impl ShString { #[must_use] pub fn len(&self) -> usize { match self { - Self(Repr::Stack(buf)) => buf.len(), + Self(Repr::Inline(buf)) => buf.len(), Self(Repr::Heap(buf)) => buf.len(), } } @@ -113,7 +113,7 @@ impl ShString { #[must_use] pub fn is_empty(&self) -> bool { match self { - Self(Repr::Stack(buf)) => buf.is_empty(), + Self(Repr::Inline(buf)) => buf.is_empty(), Self(Repr::Heap(buf)) => buf.is_empty(), } } @@ -132,7 +132,7 @@ impl ShString { #[must_use] pub fn heap_allocated(&self) -> bool { match self { - Self(Repr::Stack(_)) => false, + Self(Repr::Inline(_)) => false, Self(Repr::Heap(_)) => true, } } @@ -293,7 +293,7 @@ impl<'de, const N: usize> serde::Deserialize<'de> for ShString { #[derive(Clone)] enum Repr { - Stack(StackString), + Inline(InlineString), Heap(Box), }