Skip to content

fix: be specific about returned backing array#183

Closed
achingbrain wants to merge 1 commit into
rvagg:masterfrom
achingbrain:fix/be-specific-about-array-backing
Closed

fix: be specific about returned backing array#183
achingbrain wants to merge 1 commit into
rvagg:masterfrom
achingbrain:fix/be-specific-about-array-backing

Conversation

@achingbrain

Copy link
Copy Markdown
Contributor

Since ArrayBuffer and SharedArrayBuffer types diverged, some APIs require Uint8Array<ArrayBuffer> which makes it incompatible with the default Uint8Array (e.g. Uint8Array<ArrayBufferLike>, e.g. Uint8Array<ArrayBuffer | SharedArrayBuffer>) so update the return type of encode to make clear what type of backing buffer is used.

This means that onward code doesn't need to check the backing type.

Since `ArrayBuffer` and `SharedArrayBuffer` types diverged, some
APIs require `Uint8Array<ArrayBuffer>` which makes it incompatible
with the default `Uint8Array` (e.g. `Uint8Array<ArrayBufferLike>`,
e.g. `Uint8Array<ArrayBuffer | SharedArrayBuffer>`) so update the
return type of `encode` to make clear what type of backing buffer
is used.

This means that onward code doesn't need to check the backing type.
@achingbrain achingbrain force-pushed the fix/be-specific-about-array-backing branch from 3c399df to e233f99 Compare July 3, 2026 08:24
@rvagg

rvagg commented Jul 3, 2026

Copy link
Copy Markdown
Owner

Would you mind just limiting this to the json path? Narrowing the various encode paths is a breaking change and I don't think there's a good reason to stop supporting the caller giving a SharedArrayBuffer to write in to, that's perfectly valid today. Since we allocate for the json path we can easily be precise, but where we accept a sink as an input, restricting what you can write to is a step backwards.

I think we might be able to achieve what you want with some generics hackery, make encode() and jsonEncode() be clear about what they return, a Uint8Array<ArrayBuffer> because we're responsible for it, but for encodeInto leave it as an Uint8Array<ArrayBufferLike>. Then we don't lose the flexibility of "just write to my thing please".

@rvagg

rvagg commented Jul 3, 2026

Copy link
Copy Markdown
Owner

@achingbrain see if #184 does what you want, I think it solves for both situations

@achingbrain

Copy link
Copy Markdown
Contributor Author

Fair enough, completely agree on keeping encodeInto permissive, that was an oversight on my part.

#184 looks good to me.

rvagg added a commit that referenced this pull request Jul 4, 2026
@rvagg rvagg closed this Jul 4, 2026
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

Successfully merging this pull request may close these issues.

2 participants