Skip to content

feat: add convenience fns and FromStr impl to CowStr#2

Merged
nberlette merged 6 commits into
mainfrom
feat/cow-str-methods
Dec 24, 2025
Merged

feat: add convenience fns and FromStr impl to CowStr#2
nberlette merged 6 commits into
mainfrom
feat/cow-str-methods

Conversation

@nberlette

Copy link
Copy Markdown
Owner
  • 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

Copilot AI review requested due to automatic review settings December 24, 2025 00:25
@nberlette nberlette added the enhancement New feature or request label Dec 24, 2025
@nberlette nberlette self-assigned this Dec 24, 2025
@nberlette nberlette added the enhancement New feature or request label Dec 24, 2025
@nberlette nberlette merged commit 6f88aaf into main Dec 24, 2025
3 checks passed
@nberlette nberlette deleted the feat/cow-str-methods branch December 24, 2025 00:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FromStr trait for CowStr to enable parsing from string slices
  • Adds helper methods (try_inline, inline, force_inline, from_char) for creating inline CowStr variants
  • Corrects deprecation version from "0.4.0" to "0.2.0" for the into_owned method

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.

Comment thread src/cow_str.rs
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;
}

Copilot AI Dec 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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();

Copilot uses AI. Check for mistakes.
Comment thread src/cow_str.rs
type Err = ();

#[inline(always)]
fn from_str(s: &str) -> Result<Self, Self::Err> {

Copilot AI Dec 24, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants