Repository navigation
feat(ts): compare and hash byte arrays, slices, and vectors as std does - #276
Conversation
Quest-Claim: a6b626a4299b91c4
`==` and `!=` of bytes in any two forms, and a `[u8; N]`, `&[u8]`, or `Vec<u8>` as a hash or B-tree key, alone or in a tuple, `Option`, or `Arc`, translate through `rt.eqBytes` and `rt.hashBytes`, which read each form at run time as `rt.cmpBytes` does. Every form hashes its length as a `usize` and then its bytes, as std's `[T; N]` hashes as its slice, so a `Vec<u8>` key is found by a `&[u8]`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the array Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs # ts/fixtures/lang/src/lib.rs # ts/fixtures/reject/census.md # ts/fixtures/reject/expected.txt # ts/fixtures/reject/ts.toml # ts/src/lower/collections.rs
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 51 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: c8fe53b.
No actionable bugs found. Centralizing byte equality/hash alongside cmpBytes is a sound direction: logical length and ByteSlice offsets are respected, and the length-prefixed byte stream is consistent across arrays, slices and vectors. The lookup adaptation preserves boxed queries and Arc wrapping, while byte-form equivalence remains limited to collection lookups rather than weakening Borrow lowering.
The fixtures cover cross-form comparisons/lookups, composite keys, offset slices and deterministic hash digests; unsupported returned array references remain explicitly separated into follow-ups. Reusing byte views avoids unnecessary lookup-key copies.
Verification limits: source/generated-fixture inspection and an independent static review only. I did not run the translator, differential tests or CI; reported test results were not independently verified.
# Conflicts: # ts/fixtures/reject/census.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closes the quest quest/m3/ts-byte-keys.md.
Problem
rust2-ts refused
==and!=of byte arrays,Vec<u8> == Vec<u8>,==across byte forms (Vec<u8> == &[u8],[u8; N] == [u8], ...), and any[u8; N],&[u8], orVec<u8>as a hash key, alone or in a tuple,Option, orArc(44 refusals with spans in the new fixture cases, such as "hashing[u8; 2]as a key", "unsupported call to<[u8; 2] as std::cmp::PartialEq>::eq", "HashMap::getofHashMap<Vec<u8>, usize>by[u8]", and "HashMap::getof a reference to an array of bytes it is generic over").Approach
rt.eqBytesandrt.hashBytesread each byte form at run time (typed array,rt.ByteSlice,rt.ByteVec), asrt.cmpBytesdoes for order (feat(ts): translate slice windows and chunks, Vec::drain, and the order of bytes #259).eq_fnandhash_fnuse them for any byte type, so they compose witheqTuple,hashOption,hashArc, and the rest.PartialEq::eq/nebetween any two byte forms maps tort.eqBytes.[u8] == [u8]keeps the library'srt.byteSliceEq.impl Hash for [T; N]callsHash::hash(&self[..], state)(core/src/array/mod.rs:348), and[T]'s callswrite_length_prefixand thenu8::hash_slice, which writes the bytes (core/src/hash/mod.rs:931,:817). So all three forms write the length as ausizeand then the bytes. The brief's first claim, that[u8; N]has no length prefix, was wrong and was later corrected.DefaultHasher::newdigests of each form, compared with native wasm32, prove it. Dropping the prefix makes the differential report 414 differences.same_keytreats any two byte forms as one key, so aVec<u8>key is found by a&[u8]and a[u8; 2]key by a&[u8]. A lookup takes its key by reference, which for a[u8; N]key held as a typed array (throughBoxes andArcs) is anrt.ByteSlice, sort.byteSliceViewpasses a view of its bytes.bytes_acrossletsPartialEq,Hash::hash, and collection lookups (get,get_mut,contains_key,contains,remove,take) take a&[u8; N], since they only read it. What a lookup returns is still checked, soHashSet<[u8; 2]>::getstays refused. A new reject case covers that.hashes::bytes(digests of each form, empty arrays and slices, inside a tuple,Option, andArc, andRandomState::hash_oneequal across forms) andkeys::bytes(==/!=across forms, hash maps keyed by[u8; 2],Vec<u8>,&[u8],(Option<[u8; 2]>, Vec<u8>),Arc<Vec<u8>>,Arc<[u8; 2]>,Box<[u8; 2]>, andHashSet<&[u8; 2]>, B-tree lookups by array and by slice, and lookups by a borrowed&[u8]), run differentially against native wasm32.Impact
rt.eqBytesandrt.hashBytes. No breaking change.Alternatives
byteSliceView: this allocates, and the view already reads as aUint8Array.Hasher.wordcall: this would probably be faster, but I didn't measure it, so I kept one byte per call. SipHash consumes a byte stream, so either gives the same values.Choices made without asking
RandomState's keys are random on both sides, sohash_onevalues can't be printed. The fixture comparesDefaultHasher::newdigests with native directly, and checks thathash_oneagrees across byte forms.BuildHasherDefaultwould allow a directhash_onecomparison, but it doesn't translate yet.byte_keys(HashMap<Vec<u8>, u8>) now translates, so it becameset_keysover aBTreeSet<u8>key. That keeps the "hashing/comparing as a key" refusal covered.Follow-ups
keys,iter,get_key_value,Entry::key, andHashSet::get/iterof a[u8; N]key, still refused with a span.<[T; N]>::as_slice, refused, which came up while writing the fixture.Review
Codex
gpt-6.1-sol, medium effort, viaquest review. The reviewed head isc8fe53bda1a867d6c71a86be5c89f9e7ebd6776b.12f06002) had 2 findings, both fixed in8ad812d4:&Box<[u8; 2]>was wrapped inbyteSliceView, which throws on aUint8Array. Now only a lookup whoseQis itself a byte array or slice gets the view. A differential case coversboxed.get(&Box::new(key)).same_keylet theBorrowmapping return aByteVecforVec<u8>::borrow() -> &[u8].same_keyis back to main's definition, and the byte equivalence lives only in the lookup check. That borrow refuses with a span, as on main. Translating it is a separate std gap.8ad812d4) had no findings.c8fe53bd, a merge oforigin/mainwith hand-resolved conflicts incollections.rs, the harness case list, and the reject roots) had no findings.just cipassed locally onc8fe53bd.Merge of main at
c2f8edf9(#275) intoc8fe53bd, as54766971: took main's reject census and regenerated it (this PR'sBTreeSetkey andHashSet::getrefusal on top of #275's). Recorded skip, too trivial to review;just cipassed on54766971.(written by Claude Opus 5.5)
🤖 Generated with Claude Code