anthropics / anthropics/buffa

Allow `ProtoString` implementations without requiring `From<&str>` for every lifetime

Open
#441 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
883
Forks
88
Avg merge
3d 19h
Merged PRs (30d)
42

Description

Could `ProtoString` replace its `for<'a> From<&'a str>` requirement with a dedicated method for copying borrowed text into owned storage?

I maintain `flexstr`. It supports borrowed strings, inline storage, and reference-counted storage. Its existing `From<&str>` implementation borrows, tying the input lifetime to the output lifetime.

`SharedStr` can safely copy temporary text into owned storage, but implementing buffa’s higher-ranked `From<&str>` bound would require changing that existing conversion’s semantics making it always need to own. A new copying `From` implementation would overlap with the borrowing implementation.

### Suggested approach

Keep `From` and add an overridable method to the `ProtoString` trait such as:

```rust
fn copy_from_str(value: &str) -> Self {
Self::from(String::from(value))
}
```

JSON helpers and generated view-to-owned conversions could use this method. Binary decoding already has the appropriate extension point in `from_wire`.

There is a similar precedent in [buffa’s design](https://github.com/anthropics/buffa/blob/main/DESIGN.md#12-pluggable-owned-types--protostring-and-protobytes): `ProtoBytes` deliberately omits `From<&[u8]>` because `bytes::Bytes` only supports that conversion for static slices.

Thoughts?

Contributor guide

Open the contributing guide

Research direction

Start by reading the ProtoString trait and tracing the JSON helpers and generated view-to-owned conversions that currently depend on the From<&str> requirement. Compare the proposed extension point with from_wire and the ProtoBytes precedent in DESIGN.md; done means the API direction and affected implementation and test scope are agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.