Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Consider providing StrBytes interface #373

Closed
Diggsey opened this issue Feb 25, 2020 · 4 comments
Closed

Feature: Consider providing StrBytes interface #373

Diggsey opened this issue Feb 25, 2020 · 4 comments

Comments

@Diggsey
Copy link

Diggsey commented Feb 25, 2020

This would act like the Bytes object but would guarantee that the contents are valid utf-8. It would not implement Buf but would deref to &str and have conversions to the String type.

There would be TryFrom/TryInto conversions from Bytes to StrBytes which would scan the string for validity but would not copy it. There would be zero-cost From/Into conversions from StrBytes to Bytes. There would be an unsafe zero-cost conversion method from Bytes to StrBytes for when the bytes are already known to be valid.

It would be convenient (but not essential) if there was a way to general substring-returning operations on a StrBytes that themselves returned a StrBytes.

This would enable zero-copy string deserialization in the same way as for raw bytes.

@carllerche
Copy link
Member

Does something like this work? https://docs.rs/string/0.2.1/string/

@Diggsey
Copy link
Author

Diggsey commented Feb 25, 2020

Yes. 👍

@Diggsey Diggsey closed this as completed Feb 25, 2020
@stackinspector
Copy link

The String<T> in the crate string has no serde support implementation. When I tried to implement support for serde, I found that not knowing whether the internal bytes are borrowed or owned or something like Bytes could lead to additional overhead during deserialization. It would be much easier to implement serde support if a wrapper was created on a deterministic Bytes.

@Darksonn
Copy link
Contributor

Darksonn commented Oct 6, 2022

The bytestring crate provides serde support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants