Repository navigation
feat(ts): translate Cow<[T]> and Wrapping<T> with their std impls - #326
Merged
Merged
Conversation
Quest-Claim: fcba5a6fbea67914
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/caller.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs # ts/fixtures/reject/census.md # ts/src/lower/collections.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A borrowed Cow's clone gets its own rt.Borrowed, whose slice a &mut writes, and a variable assigned through a cast to its variant's type is declared with let. 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/reject/census.md # ts/fixtures/reject/expected.txt
…f it shares 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/reject/census.md
kixelated
marked this pull request as ready for review
October 31, 2026 05:09
This was referenced Oct 31, 2026
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.
Closes the quest quest/m3/ts-cow-slices.md.
Problem
Constructing a
Cow<[T]>was refused (Cow<str>is already a JS string), and everyWrapping<T>was refused as an unsupported type from another crate, so none of their std impls translated. Reproduced on a scratch crate: 55 refusals, each with its span.Approach
Cow<[T]>, a representation decided by the orchestrator, pending maintainer review:Cow<[T]>is its bareT[], and a borrowed one is anrt.Borrowedholding the slice. ACow<[u8]>is anrt.ByteVecor anrt.ByteSlice, which are already distinct classes.to_mutstoresrt.cowIntoOwnedof theCowin the&mut's place, then lends thatVec. A borrowed one clones each item by itsClone; an owned one is returned as it is.Cloneof a borrowed one is a newrt.Borrowedof the same slice, so a&mutto the payload, whether stored directly, passed on as anrt.Place, or both, writesslicein place on its ownCowonly.Cloneof an owned one clones itsVec. Drop drops only an owned one's items.into_owned,Deref,AsRef,Borrow,Default,From(aVec, a&Vec, a slice, an array),Vec::from, andcollect(std's collects the ownedVec) are all supported. Amatchand a pattern read the variant byinstanceof.PartialEq,PartialOrd,Ord, andHashgo through the slice it derefs to, byrt.cowByandrt.hashCow. ACow'sneis!eq, and the slice'seqstill calls each item's ownne(feat(ts): compare through a held type's own ne, as std's slices, tuples, and Arcs do #316). This covers slices,Vecs, and arrays on either side, andCow<[u8]>against every form of bytes.Cowvalue itself, so a store to it assigns the variable through a cast to the variant's type.assigned_nameinmir/mod.rsnow looks through that cast, so the variable is declaredlet; before, it wasconstand the store threw at runtime.Cow::is_borrowedandis_ownedare unstable at the pinned toolchain, so a slice's are refused as unsupported calls. TheCow<str>refusal message ("either is the same JS string") is no longer used for slices.Wrapping<T>erases to its integer, asReverseandNonZerodo:+,-,*, and-xcall the integer'swrapping*runtime functions./and%use the newrt.*WrappingDivandrt.*WrappingRem, soMIN / -1wraps and only a zero divisor panics, with std's message.bits - 1; a constant amount is folded.!,&,|, and^translate too.Hash,Debug,Display, and the radix formats,SumandProduct(wrapping),Default,Clone, and derived impls of structs holding one all translate. Hashed keys look through it as they do throughReverse.Tests, all differential against native wasm32 in the
langfixture:wraps::{u8s..isizes}: 24 kinds per width.caller.tsadds every kind at every width over operands at 0, 1, either end, and around the sign bit (soMIN / -1is hit), plus values past 2^64 for the 128-bit widths. Shift amounts are at, past, and many times past the width, including amounts above 2^24.cows::slices: construction each way, a payload replaced directly and through a helper with reads through the same borrow,to_mutof a local, an element, and a field (each checking that the borrowed source is unchanged), comparisons against every sequence on either side, B-tree and hash keys, andmatch.Tally, an item that logs its clones and drops, shows what a borrowed or ownedCowclones and drops throughclone,to_mut, andinto_owned.cows::bytes: the same coverage forCow<[u8]>, payload replacement included.collections::own_nes:Wrapping<Picky>andCow<[Picky]>, whose items'eqandnedisagree.Cow's unstableis_borrowedin place of the old refusal ofCow<[u8]>itself.Impact
Cow<[T]>andWrapping<T>translate where they were refused.rt.Borrowed,rt.Cow,rt.ByteCow,rt.cowSlice,rt.byteCowSlice,rt.cowIntoOwned,rt.byteCowIntoOwned,rt.cowClone,rt.byteCowClone,rt.cowDrop,rt.cowBy,rt.hashCow, andrt.{i8,i16,i32,i64,i128,isize}Wrapping{Div,Rem}. Nothing is breaking.ts/README.mddocuments both types, and its design notes add a line besideCow<str>'s.Alternatives
Representation of
Cow<[T]>:Cow<str>is a bare string: lost becauseto_mutcould not tell a borrow apart from an ownedVec, so it would clone on every call (O(n^2) over repeated calls), and amatchon the variant would be refused.{ owned, value }: lost because it wraps everyCow, owned ones included, and unwraps it at every read.Follow-ups
None.
Review
quest review, Codex gpt-6.1-sol, medium effort.479369fa: a borrowedCow's payload replaced through&mutstored to a readonlyslice. Fixed in305e9dc3, which also covered the owned and byte variants assigning to a cast of aconst.305e9dc3: the same payload passed to a helper as anrt.Placestill wrote the readonlyslice. Fixed inb1f0adf9:sliceis writable, each borrowed clone gets its own wrapper, and a cast assignment target counts as a write.ec8c6ed2:305e9dc3's rebuilding of the wholeCowbroke wrapper identity for a borrow a helper had captured. Fixed ineb6083ed, which drops that rebuilding and writesslicein place.f8dd028e(with origin/main merged): no findings.just cipassed locally onf8dd028e.🤖 Generated with Claude Code
(written by Claude Opus 5.5)