Switch from ArrayBuffer to ByteArray #187
Merged
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.
According to The Big O of Code Reviews, this is a O(n) change.
ubrn lowers most types to an
ArrayBuffer
and then maps it to ajsi::ArrayBuffer
, and then on toRustBuffer
.wasm-bindgen
maps aUint8Array
on to aVec<u8>
.In order to lower types to a
Uint8Array
for WASM andArrayBuffer
for JSI, some amount of bundler violence was needed. Given we're using multiple bundlers to package up the frontend Typescript, and we're not really in control of what bundler the developer uses, this doesn't seem like a viable approach.Instead, this PR changes ubrn to lower types down to a
Uint8Array
, so that both WASM and React Native use the same underlying representation of byte arrays.Unfortunately, jsi doesn't expose API for typed arrays (it may in the future), so we look up the
buffer
property to get an underlyingArrayBuffer
.On the way back, an object of type
{ buffer: ArrayBuffer; }
is returned, which is enough to make the uniffi work and the typescript compiler think it that a typed array has come back from Rust/C++.Most of this PR was mechanical refactoring (e.g.
ArrayBuffer
renamed toUniffiByteArray
) but there are enough manual changes which merit it being O(n).