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

Introduce a byte-type #68

Closed
TimDiekmann opened this issue Aug 5, 2020 · 4 comments
Closed

Introduce a byte-type #68

TimDiekmann opened this issue Aug 5, 2020 · 4 comments

Comments

@TimDiekmann
Copy link
Member

The allocator-api currently only uses u8 as representation for a byte. I like to discuss if a type is worth to introduce (lets call it byte here). This is basically an alternative to #66.

Advantages:

  • u8 is an unsigned number, which must not have uninitialized bits (see Clarify unsafe requirements of AllocRef #65)
  • Could be used in (many?) other places
  • Could have own methods, which are dedicated to byte operations (byte or [byte] + &, *const, *mut, NonNull)

Downsides:

  • Introduces a new type or type alias
@TimDiekmann TimDiekmann mentioned this issue Aug 5, 2020
18 tasks
@Wodann
Copy link

Wodann commented Aug 5, 2020

For reference, it sounds like it might be analogous to the C++ std::byte type

@Amanieu
Copy link
Member

Amanieu commented Aug 5, 2020

I don't think this is worth the added complexity of another type. We use u8 everywhere else for this in the standard library (e.g. I/O buffers) and it has worked just fine so far.

@Ixrec
Copy link

Ixrec commented Aug 6, 2020

Given the C++ comparison, it's worth pointing out that C++ added this type specifically to deal with the fact that char is a terrible type for representing raw bytes, which is not a mistake Rust ever made. As the cppreference page nicely summarizes:

Like char and unsigned char, it can be used to access raw memory occupied by other objects (object representation), but unlike those types, it is not a character type and is not an arithmetic type. A byte is only a collection of bits, and the only operators defined for it are the bitwise ones.

(and for completeness, here's the proposal for adding it, which is basically spelling that out in more detail)

u8 is already unsigned and not a character type, and Rust deliberately lacks the implicit integer promotions/coercions that make "not an arithmetic type" super appealing, so AFAIK most of C++'s motivation for a separate byte type simply doesn't apply to Rust.

The one part of C++'s motivation that does apply is that with raw bytes, only bitwise operators really make sense, and using one of the +-*/ arithmetic operators is likely a mistake, hence std::byte only supports the former. But that doesn't seem like enough of a problem in Rust to justify a separate type on its own.


I have no strong opinion on whether Rust should have a byte type. I could easily see unsafe code guidelines or allocators or whatever ending up in a place where having one allows better UB checking or static UB prevention. But I am confident that the prior art of C++ is largely irrelevant to this question.

@TimDiekmann
Copy link
Member Author

Closing as of #68 (comment) and #66 (comment).

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