Unify CBOR std::byte and uint8_t byte-string handling#2652
Merged
Conversation
Follow-up to #2650. CBOR handled std::byte ranges with one concept-based specialization but uint8_t ranges only via enumerated std::vector<uint8_t> / std::array<uint8_t, N> specializations, so other contiguous uint8_t ranges (e.g. std::span<uint8_t>) silently fell through to RFC 8746 typed-array encoding instead of a byte string. Collapse the four byte-string writers and three byte-string readers into one read/write pair each, constrained on glz::contiguous_byte_range (byte_like: std::byte, unsigned char, uint8_t), and widen the generic array specs' exclusion to match. Every contiguous byte-like range now encodes as a CBOR byte string (major type 2) and the variants are cross-readable on the wire, regardless of element type or container. Net -293/+72 lines. Adds regression tests for identical byte/uint8 encodings, span<uint8_t> as a byte string, and byte<->uint8 cross-readability for vectors and fixed arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #2650. That PR fixed
std::byteand fixedstd::array<char, N>handling across the binary codecs, but left CBOR'sstd::byteanduint8_tbyte handling implemented two different ways:std::byteranges were handled by a single concept-based specialization (any contiguousstd::byterange → byte string).uint8_tranges were handled only by enumerated specializations forstd::vector<uint8_t>andstd::array<uint8_t, N>.So any other contiguous
uint8_trange (e.g.std::span<uint8_t>) silently fell through to the generic array writer and was encoded as an RFC 8746 typed array instead of a byte string, while the equivalentstd::span<std::byte>was a byte string. MsgPack already treats these uniformly viabyte_like; CBOR was the outlier.Change
Collapse the duplicated specializations into one read/write pair each, constrained on the existing
glz::contiguous_byte_rangeconcept (byte_like=std::byte,unsigned char,uint8_t), and widen the generic array specializations' exclusion to match:std::byterange +std::vector<uint8_t>+std::array<std::byte, N>+std::array<uint8_t, N>) → 1.std::byterange +std::vector<uint8_t>+std::array<uint8_t, N>) → 1. The existingstd::bytereader already handled both resizable and fixed-size targets with themax_array_sizesafety checks, so the twouint8_treaders were redundant subsets of it.Net −293 / +72 lines.
Behavior
Every contiguous byte-like range now encodes as a CBOR byte string (major type 2) and the variants are cross-readable on the wire, regardless of element type or container.
std::vector<uint8_t>/std::array<uint8_t, N>round-trips and wire bytes are unchanged.One intentional wire-format change: previously-uncovered
uint8_tcontainers such asstd::span<uint8_t>now encode as a byte string instead of an RFC 8746 typed array, making them consistent withstd::span<std::byte>. (Numeric typed arrays for other element types, e.g.int32_t/float, are unaffected.)Tests
All existing CBOR tests pass (239), plus 4 new regression tests: identical byte/uint8 encodings,
std::span<uint8_t>as a byte string, and byte ↔ uint8 cross-readability for vectors and fixed arrays. Verified locally under clang-std=c++23, plain and ASan+UBSan (243 tests / 961 asserts, 0 failures).