feat: add convenience fns and FromStr impl to CowStr#2
Conversation
nberlette
commented
Dec 24, 2025
- feat: add FromStr impl to CowStr
- fix: update version typo in deprecation
- chore: switch toolchain back to nightly to use unstable fmt options
- feat(cow_str): add try_inline, inline, force_inline helpers to CowStr
- fix(cow_str): fix lifetime errors in CowStr's FromStr impl
- chore: fmt
There was a problem hiding this comment.
Pull request overview
This PR adds convenience functions for creating inline CowStr instances and implements the FromStr trait. It also corrects a version number in a deprecation notice and updates formatting to use nightly toolchain conventions.
- Implements
FromStrtrait forCowStrto enable parsing from string slices - Adds helper methods (
try_inline,inline,force_inline,from_char) for creating inlineCowStrvariants - Corrects deprecation version from "0.4.0" to "0.2.0" for the
into_ownedmethod
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/cow_str.rs | Implements FromStr trait, adds inline creation helpers, fixes deprecation version, and adds variant checking methods |
| rust-toolchain.toml | Switches toolchain from stable to nightly |
| .github/workflows/ci.yml | Updates quote style from single to double quotes for workflow descriptions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let src = s.as_ref().as_bytes(); | ||
| let mut len = src.len(); | ||
| if len > MAX_INLINE_STR_LEN { | ||
| len = MAX_INLINE_STR_LEN; | ||
| } |
There was a problem hiding this comment.
Truncating UTF-8 strings at arbitrary byte boundaries can split multi-byte characters, resulting in invalid UTF-8. Use character boundary checking (e.g., s.as_ref().char_indices()) to find a safe truncation point instead of slicing at MAX_INLINE_STR_LEN bytes.
| 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(); |
| type Err = (); | ||
|
|
||
| #[inline(always)] | ||
| fn from_str(s: &str) -> Result<Self, Self::Err> { |
There was a problem hiding this comment.
The FromStr implementation returns Result<Self, ()> which provides no error information. Since this implementation never fails (it falls back to Owned when inlining fails), consider returning Self directly by implementing From<&str> instead, or use a meaningful error type if error cases are anticipated.