From 8b1fd457a82062723715c7e1e7bb8a698ec365d1 Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Sun, 15 May 2022 20:08:14 +0100 Subject: [PATCH] Mark several functions as inline and must_use --- src/strings.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/strings.rs b/src/strings.rs index df5aa63..f92188c 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -21,6 +21,8 @@ use sqlx::{ use buf::{StackString, HeapString}; +// TODO: inlining + /// A non-growable string where strings 22 bytes or shorter are stored on the stack and longer /// strings are stored on the heap. /// @@ -45,6 +47,8 @@ pub struct ShString(Repr); impl ShString { /// Creates a new `ShString` from the given string slice, putting it on the stack if possible /// or creating a new heap allocation otherwise. + #[inline] + #[must_use] pub fn new_from_str(s: &str) -> Self { match StackString::from_str(s) { Some(stack_buf) => Self(Repr::Stack(stack_buf)), @@ -54,6 +58,8 @@ impl ShString { /// Creates a new `ShString` from the given owned `String`, moving the string data onto the /// stack if possible or reusing the `String`'s heap allocation otherwise. + #[inline] + #[must_use] pub fn new_from_string(s: String) -> Self { match StackString::from_str(&s) { Some(stack_buf) => Self(Repr::Stack(stack_buf)), @@ -62,6 +68,8 @@ impl ShString { } /// Creates a new `ShString` from the given `Cow`. + #[inline] + #[must_use] pub fn new_from_cow_str(s: Cow) -> Self { match s { Cow::Borrowed(s) => Self::new_from_str(s), @@ -70,6 +78,8 @@ impl ShString { } /// Returns a string slice for the underlying string data. + #[inline] + #[must_use] pub fn as_str(&self) -> &str { match self { Self(Repr::Stack(buf)) => buf.as_str(), @@ -78,6 +88,8 @@ impl ShString { } /// Returns a mutable string slice for the underlying string data. + #[inline] + #[must_use] pub fn as_str_mut(&mut self) -> &mut str { match self { Self(Repr::Stack(buf)) => buf.as_str_mut(), @@ -86,6 +98,8 @@ impl ShString { } /// Consumes the `ShString` and converts it to a heap-allocated `String`. + #[inline] + #[must_use] pub fn into_string(self) -> String { match self { Self(Repr::Stack(buf)) => buf.into_string(), @@ -100,6 +114,8 @@ impl ShString { /// let s = ShString::<22>::new_from_str("こんにちは"); /// assert_eq!(s.len(), 15); /// ``` + #[inline] + #[must_use] pub fn len(&self) -> usize { match self { Self(Repr::Stack(buf)) => buf.len(), @@ -117,6 +133,8 @@ impl ShString { /// let s2 = ShString::<22>::new_from_str("Hello"); /// assert!(!s2.is_empty()); /// ``` + #[inline] + #[must_use] pub fn is_empty(&self) -> bool { match self { Self(Repr::Stack(buf)) => buf.is_empty(), @@ -134,6 +152,8 @@ impl ShString { /// let s2 = ShString::<22>::new_from_str("This string is 23 bytes"); /// assert!(s2.heap_allocated()); /// ``` + #[inline] + #[must_use] pub fn heap_allocated(&self) -> bool { match self { Self(Repr::Stack(_)) => false, @@ -145,60 +165,70 @@ impl ShString { impl ops::Deref for ShString { type Target = str; + #[inline] fn deref(&self) -> &Self::Target { self.as_str() } } impl ops::DerefMut for ShString { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { self.as_str_mut() } } impl AsRef for ShString { + #[inline] fn as_ref(&self) -> &str { self } } impl borrow::Borrow for ShString { + #[inline] fn borrow(&self) -> &str { self } } impl borrow::BorrowMut for ShString { + #[inline] fn borrow_mut(&mut self) -> &mut str { self } } impl<'a, const N: usize> From<&'a str> for ShString { + #[inline] fn from(s: &'a str) -> Self { Self::new_from_str(s) } } impl From for ShString { + #[inline] fn from(s: String) -> Self { Self::new_from_string(s) } } impl<'a, const N: usize> From> for ShString { + #[inline] fn from(s: Cow<'a, str>) -> Self { Self::new_from_cow_str(s) } } impl From> for String { + #[inline] fn from(s: ShString) -> Self { s.into_string() } } impl PartialEq> for ShString { + #[inline] fn eq(&self, other: &ShString) -> bool { **self == **other } @@ -207,18 +237,21 @@ impl PartialEq> for ShString { impl Eq for ShString {} impl PartialOrd> for ShString { + #[inline] fn partial_cmp(&self, other: &ShString) -> Option { (**self).partial_cmp(&**other) } } impl Ord for ShString { + #[inline] fn cmp(&self, other: &Self) -> Ordering { (**self).cmp(&**other) } } impl Hash for ShString { + #[inline] fn hash(&self, state: &mut H) { (**self).hash(state); } @@ -227,18 +260,21 @@ impl Hash for ShString { impl FromStr for ShString { type Err = Infallible; + #[inline] fn from_str(s: &str) -> Result { Ok(Self::new_from_str(s)) } } impl fmt::Debug for ShString { + #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } impl fmt::Display for ShString { + #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&**self, f) }