-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add convenience fns and FromStr impl to CowStr #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4134cc3
feat: add FromStr impl to CowStr
nberlette e965ea9
fix: update version typo in deprecation
nberlette f7e569b
chore: switch toolchain back to nightly to use unstable fmt options
nberlette 5ffeb61
feat(cow_str): add try_inline, inline, force_inline helpers to CowStr
nberlette 517a25a
fix(cow_str): fix lifetime errors in CowStr's FromStr impl
nberlette 7b48c1b
chore: fmt
nberlette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [toolchain] | ||
| channel = "stable" | ||
| channel = "nightly" | ||
| components = ["rustfmt", "clippy", "cargo"] | ||
| profile = "minimal" | ||
| profile = "minimal" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ use core::mem::transmute_copy; | |||||||||||||||||||||||||||||||||||||||||||
| use core::ops::Deref; | ||||||||||||||||||||||||||||||||||||||||||||
| use core::ops::DerefMut; | ||||||||||||||||||||||||||||||||||||||||||||
| use core::str; | ||||||||||||||||||||||||||||||||||||||||||||
| use core::str::FromStr; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| use crate::inline_str::*; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -143,9 +144,7 @@ impl<'i> CowStr<'i> { | |||||||||||||||||||||||||||||||||||||||||||
| self.len() == 0 | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Converts the `CowStr` into an owned `String`, cloning the data if | ||||||||||||||||||||||||||||||||||||||||||||
| /// necessary. | ||||||||||||||||||||||||||||||||||||||||||||
| #[deprecated(since = "0.4.0", note = "use `into_string` instead")] | ||||||||||||||||||||||||||||||||||||||||||||
| #[deprecated(since = "0.2.0", note = "use `into_string` instead")] | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn into_owned(self) -> String { | ||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -155,6 +154,8 @@ impl<'i> CowStr<'i> { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Converts the `CowStr` into an owned `String`, cloning the data if | ||||||||||||||||||||||||||||||||||||||||||||
| /// necessary. | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn into_string(self) -> String { | ||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -165,6 +166,18 @@ impl<'i> CowStr<'i> { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| impl<'i> FromStr for CowStr<'i> { | ||||||||||||||||||||||||||||||||||||||||||||
| type Err = (); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||||||||||||||||||||||||||||||||||||||||||||
| match InlineStr::try_from(s) { | ||||||||||||||||||||||||||||||||||||||||||||
| Ok(inline) => Ok(CowStr::Inlined(inline)), | ||||||||||||||||||||||||||||||||||||||||||||
| Err(_) => Ok(CowStr::Owned(s.to_string().into_boxed_str())), | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| impl<'i> Display for CowStr<'i> { | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -396,6 +409,77 @@ impl<'i> From<CowStr<'i>> for String { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(not(feature = "is_variant"))] | ||||||||||||||||||||||||||||||||||||||||||||
| impl<'i> CowStr<'i> { | ||||||||||||||||||||||||||||||||||||||||||||
| /// Returns `true` if the `CowStr` is the `Owned` variant. | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub const fn is_owned(&self) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||
| matches!(self, CowStr::Owned(_)) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Returns `true` if the `CowStr` is the `Inlined` variant. | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub const fn is_inlined(&self) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||
| matches!(self, CowStr::Inlined(_)) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Returns `true` if the `CowStr` is the `Borrowed` variant. | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub const fn is_borrowed(&self) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||
| matches!(self, CowStr::Borrowed(_)) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| impl CowStr<'_> { | ||||||||||||||||||||||||||||||||||||||||||||
| /// Attempts to create an inline `CowStr` from a value that can be converted | ||||||||||||||||||||||||||||||||||||||||||||
| /// to a string slice via an `AsRef<str>` impl. | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// Returns an error if the string is too long to be inlined. | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn try_inline<'i, T: 'i + AsRef<str>>( | ||||||||||||||||||||||||||||||||||||||||||||
| s: T, | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<CowStr<'i>, StringTooLongError> { | ||||||||||||||||||||||||||||||||||||||||||||
| let inline = InlineStr::try_from(s.as_ref())?; | ||||||||||||||||||||||||||||||||||||||||||||
| Ok(CowStr::Inlined(inline)) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Creates an inline `CowStr` from a value that can be converted to a string | ||||||||||||||||||||||||||||||||||||||||||||
| /// slice via an `AsRef<str>` impl, panicking if the string is too long to be | ||||||||||||||||||||||||||||||||||||||||||||
| /// inlined. | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// # Panics | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// Panics if the string length exceeds [`MAX_INLINE_STR_LEN`]. | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn inline<'i, T: 'i + AsRef<str>>(s: T) -> CowStr<'i> { | ||||||||||||||||||||||||||||||||||||||||||||
| let inline = | ||||||||||||||||||||||||||||||||||||||||||||
| InlineStr::try_from(s.as_ref()).expect("String too long to inline!"); | ||||||||||||||||||||||||||||||||||||||||||||
| CowStr::Inlined(inline) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Forcibly creates an inline `CowStr` from a given value that can be | ||||||||||||||||||||||||||||||||||||||||||||
| /// converted to a string slice via an `AsRef<str>` impl, truncating it if | ||||||||||||||||||||||||||||||||||||||||||||
| /// necessary to fit within the maximum inline length. | ||||||||||||||||||||||||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn force_inline<'i, T: 'i + AsRef<str>>(s: T) -> CowStr<'i> { | ||||||||||||||||||||||||||||||||||||||||||||
| let src = s.as_ref().as_bytes(); | ||||||||||||||||||||||||||||||||||||||||||||
| let mut len = src.len(); | ||||||||||||||||||||||||||||||||||||||||||||
| if len > MAX_INLINE_STR_LEN { | ||||||||||||||||||||||||||||||||||||||||||||
| len = MAX_INLINE_STR_LEN; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+465
to
+469
|
||||||||||||||||||||||||||||||||||||||||||||
| let src = s.as_ref().as_bytes(); | |
| let mut len = src.len(); | |
| if len > MAX_INLINE_STR_LEN { | |
| len = MAX_INLINE_STR_LEN; | |
| } | |
| let s_ref = s.as_ref(); | |
| let mut len = s_ref.len(); | |
| if len > MAX_INLINE_STR_LEN { | |
| // Truncate at a valid UTF-8 character boundary so we don't split | |
| // multi-byte characters. | |
| let mut safe_len = 0usize; | |
| for (idx, ch) in s_ref.char_indices() { | |
| let ch_end = idx + ch.len_utf8(); | |
| if ch_end > MAX_INLINE_STR_LEN { | |
| break; | |
| } | |
| safe_len = ch_end; | |
| } | |
| len = safe_len; | |
| } | |
| let src = s_ref.as_bytes(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
FromStrimplementation returnsResult<Self, ()>which provides no error information. Since this implementation never fails (it falls back toOwnedwhen inlining fails), consider returningSelfdirectly by implementingFrom<&str>instead, or use a meaningful error type if error cases are anticipated.