Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Nov 25, 2021

This adds the hex->std::byte helper after the std::byte->hex helper was added in commit 9394964

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 26, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@laanwj
Copy link
Member

laanwj commented Nov 28, 2021

The long-term idea is to move from uint8_t to std::byte everywhere?

@sipa
Copy link
Member

sipa commented Nov 28, 2021

I believe that the rationale behind std::byte in the C++ standard is having a type that represents pure "memory units" for storage, without associated arithmetic. So I think it makes sense to start adopting those in our codebase whenever char/uchar are used in places that don't actually treat them as numbers or as characters of a string.

@maflcko
Copy link
Member Author

maflcko commented Nov 29, 2021

The long-term idea is to move from uint8_t to std::byte everywhere?

Yes, in places where raw bytes are handled. Though, this is mostly a style question, so there is no rush or need to switch existing code. For example, #23438 updates serialize to use Spans, and updating to std::byte at the same time was "free".

@laanwj
Copy link
Member

laanwj commented Nov 29, 2021

Concept ACK

in the C++ standard is having a type that represents pure "memory units" for storage, without associated arithmetic.

Ok. I see. It would be incredibly difficult to port bitcoin to a system that doesn't have 8-bit memory units. uint8_t just makes sense to me in that regard.
But no problem with making some code more general when it comes for free.

@sipa
Copy link
Member

sipa commented Nov 30, 2021

@laanwj I don't think the point is generality; we very much assume that a byte is 8 bits all over the place. It's more a type safety thing: std::byte indicates we're just talking about bytes, and not about numbers. They don't automatically get converted to ints or bools, for example.

@laanwj
Copy link
Member

laanwj commented Dec 8, 2021

It's more a type safety thing: std::byte indicates we're just talking about bytes, and not about numbers

Thanks for the explanation. Yes a "memory area without interpretation" abstraction makes sense. I still have a hard time grasping when to use it though. We might want to add something to developer-notes.md for guidance.
(e.g. if it requires explicit casts everywhere, we don't want to use it in cases where the memory is in fact interpreted and treated as numbers, it would just add more boilerplate)

@maflcko
Copy link
Member Author

maflcko commented Dec 8, 2021

I think a good rule of thumb would be to use it only for "raw memory" or "serialized memory". That is, any kind of memory that needs to be passed through a deserializer/parser before being useful.

For example: #23438 changes the serialize framework.

Also, fix style in the corresponding function. The style change can be
reviewed with "--word-diff-regex=."
@pk-b2
Copy link

pk-b2 commented Apr 29, 2022

ACK facd1fb

Does it make sense to mention this specifically in the Developer notes when to use std::byte?
https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#c-data-structures

@maflcko
Copy link
Member Author

maflcko commented Apr 29, 2022

Yeah, I am happy to review a pull request that changes the dev notes, though it seems unrelated to this change here, since we already use std::byte extensively in existing code, so likely I won't be changing the docs in this pull.

@laanwj
Copy link
Member

laanwj commented May 20, 2022

Code review ACK facd1fb

@laanwj laanwj merged commit 0cd1a2e into bitcoin:master May 20, 2022
@w0xlt
Copy link
Contributor

w0xlt commented May 21, 2022

Post-merge ACK facd1fb

@maflcko maflcko deleted the 2111-utilHexByte branch May 27, 2022 13:35
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 28, 2022
facd1fb refactor: Use Span of std::byte in CExtKey::SetSeed (MarcoFalke)
fae1006 util: Add ParseHex<std::byte>() helper (MarcoFalke)
fabdf81 test: Add test for embedded null in hex string (MarcoFalke)

Pull request description:

  This adds the hex->`std::byte` helper after the `std::byte`->hex helper was added in commit 9394964

ACKs for top commit:
  pk-b2:
    ACK bitcoin@facd1fb
  laanwj:
    Code review ACK facd1fb

Tree-SHA512: e2329fbdea2e580bd1618caab31f5d0e59c245a028e1236662858e621929818870b76ab6834f7ac6a46d7874dfec63f498380ad99da6efe4218f720a60e859be
@bitcoin bitcoin locked and limited conversation to collaborators May 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants