Allow `ProtoString` implementations without requiring `From<&str>` for every lifetime
- Vorherrschende Sprache
- Rust
- Sterne
- 883
- Forks
- 88
- Ø Merge
- 3 T. 19 Std.
- Gemergte PRs (30 T.)
- 42
Beschreibung
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?
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.