-
Notifications
You must be signed in to change notification settings - Fork 308
Uplift Utf8Bytes #783
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
base: master
Are you sure you want to change the base?
Uplift Utf8Bytes #783
Conversation
5b5e760
to
9a523a7
Compare
FWIW I think this is a good idea. Other than |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left a small nitpick.
src/utf8.rs
Outdated
|
||
/// Creates `Utf8Bytes` instance from slice, by copying it. | ||
pub fn copy_from_slice(data: &str) -> Self { | ||
String::from(data).into() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this call Bytes::copy_from_slice
instead and avoid having more and more code rely on the internal representation of Bytes
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good point. Does copy_from_slice
seem like a reasonable name here? I figured that the Bytes
version was probably moreso referring to the borrowed nature of data
, rather than the fact it's specifically a [_]
slice type, so I used the same name here instead of something like copy_from_str
.
This adds a Bytes wrapper with a UTF-8 validity invariant, uplifting the type found as:
bytes_utils::Str
(2.4M downloads/month)bytestring::ByteString
(1.7M downloads/month)tungstenite::Utf8Bytes
pub(crate) http::ByteStr
axum::extract::ws::Utf8Bytes
(itself a wrapper aroundtungstenite::Utf8Bytes
, so as to not depend on tungstenite in the public API)pub(crate) h2::hpack::ByteStr
async_nats::Subject
My approach here was to just copy all the API surface from
Bytes
that could be adapted (as well as a function based onString::from_utf8()
), but if this is too big to merge all at once, I could take out some of the methods and add them in a followup PR. I didn't directly copy anything from any of the aforementioned crates, in case that matters wrt licensing, though I did look a little bit at howbytestring
phrased their docs.The
Hash
impl delegates tostr as Hash
rather thanBytes as Hash
, meaning it's not hash-compatible withBytes
. This differs fromhttp::ByteStr
andtungstenite::Utf8Bytes
, but it's required, sinceUtf8Bytes
implementsBorrow<str>
(and so doestungstenite::Utf8Bytes
, which means that was probably an oversight).wrto the name:
Utf8Bytes
seemed to me the most straightforward description of what it is, and the axum case is what spurred me to open this.ByteString
seems like it could imply thatstd::string::String
isn't composed of bytes (or that it's something like bstr; not necessarily utf8), andBytesString
is a bit clunky with the geminated s.Str
is simple likeBytes
is, but doesn't match up withOsStr
orCStr
, which are slices and not owned buffers. However, I'm not dead set on anything, and would be totally fine with renaming it if that's desired.