feat: add LazyValue::as_cow_str#217
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #217 +/- ##
==========================================
- Coverage 70.60% 70.51% -0.10%
==========================================
Files 42 42
Lines 9550 9563 +13
==========================================
Hits 6743 6743
- Misses 2807 2820 +13 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Codex Review: Didn't find any major issues. Bravo. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Thanks for the contribution! The motivation behind zero-copy However, I'm hesitant to add this API to sonic-rs for a couple of reasons: API surface bloat and naming confusion
If we kept going down this path, we'd end up with a 3×2 matrix of methods (raw vs parsed × This can be implemented on the user side Users who need a let lv: LazyValue = sonic_rs::get(&input, &["key"]).unwrap();
let cow: Cow<'_, str> = match lv.as_raw_cow() {
Cow::Borrowed(s) if !s.contains('\\') => Cow::Borrowed(&s[1..s.len() - 1]),
_ => Cow::Owned(lv.as_str().unwrap().to_string()),
};It's a few more lines, but it keeps the library API lean and avoids the naming ambiguity. I'd prefer to keep |
I don't have a preference for name. Maybe it could be
I see your reasoning, but I disagree. The JsonValueTrait doesn't have functions for Cow/FastStr, only str. So this is a version of
I appreciate your burden. But this workaround is not zero-cost. |
What type of PR is this?
feat
Check the PR title.
More detailed description for this PR.
Add LazyValue::as_cow_str to support zero-copy get.
Currently,
sonic-rs::getreturnsLazyValue, butLazyValue::as_strreturns&strwhich is tied to the lifetime of theLazyValuereference instead of the input. This means that even if there are no escapes, we've to copy the str to return the value.LazyValue::as_cow_strreturns aCow<'a, str>which is tied to the lifetime of the input.