Skip to content

Commit 8fe5bf1

Browse files
Add support for number slices of type MaybeUninit<T> (#4316)
Co-authored-by: Michael Schmidt <msrd0000@gmail.com>
1 parent f90ebda commit 8fe5bf1

File tree

12 files changed

+571
-104
lines changed

12 files changed

+571
-104
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* Add `WASM_BINDGEN_TEST_DRIVER_TIMEOUT` environment variable to control the timeout to start and connect to the test driver.
1212
[#4320](https://github.com/rustwasm/wasm-bindgen/pull/4320)
1313

14+
* Add support for number slices of type `MaybeUninit<T>`.
15+
[#4316](https://github.com/rustwasm/wasm-bindgen/pull/4316)
16+
1417
### Changed
1518

1619
* Remove `once_cell/critical-section` requirement for `no_std` with atomics.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ wasm-bindgen-test = { path = 'crates/test' }
5252

5353
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
5454
js-sys = { path = 'crates/js-sys' }
55+
paste = "1"
5556
serde_derive = "1.0"
5657
wasm-bindgen-futures = { path = 'crates/futures' }
5758
wasm-bindgen-test-crate-a = { path = 'tests/crates/a' }

crates/cli/tests/reference/echo.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export function echo_vec_u32(a: Uint32Array): Uint32Array;
2525
export function echo_vec_i32(a: Int32Array): Int32Array;
2626
export function echo_vec_u64(a: BigUint64Array): BigUint64Array;
2727
export function echo_vec_i64(a: BigInt64Array): BigInt64Array;
28+
export function echo_vec_uninit_u8(a: Uint8Array): Uint8Array;
29+
export function echo_vec_uninit_i8(a: Int8Array): Int8Array;
30+
export function echo_vec_uninit_u16(a: Uint16Array): Uint16Array;
31+
export function echo_vec_uninit_i16(a: Int16Array): Int16Array;
32+
export function echo_vec_uninit_u32(a: Uint32Array): Uint32Array;
33+
export function echo_vec_uninit_i32(a: Int32Array): Int32Array;
34+
export function echo_vec_uninit_u64(a: BigUint64Array): BigUint64Array;
35+
export function echo_vec_uninit_i64(a: BigInt64Array): BigInt64Array;
2836
export function echo_vec_string(a: (string)[]): (string)[];
2937
export function echo_struct(a: Foo): Foo;
3038
export function echo_vec_struct(a: (Foo)[]): (Foo)[];
@@ -53,6 +61,14 @@ export function echo_option_vec_u32(a?: Uint32Array): Uint32Array | undefined;
5361
export function echo_option_vec_i32(a?: Int32Array): Int32Array | undefined;
5462
export function echo_option_vec_u64(a?: BigUint64Array): BigUint64Array | undefined;
5563
export function echo_option_vec_i64(a?: BigInt64Array): BigInt64Array | undefined;
64+
export function echo_option_vec_uninit_u8(a?: Uint8Array): Uint8Array | undefined;
65+
export function echo_option_vec_uninit_i8(a?: Int8Array): Int8Array | undefined;
66+
export function echo_option_vec_uninit_u16(a?: Uint16Array): Uint16Array | undefined;
67+
export function echo_option_vec_uninit_i16(a?: Int16Array): Int16Array | undefined;
68+
export function echo_option_vec_uninit_u32(a?: Uint32Array): Uint32Array | undefined;
69+
export function echo_option_vec_uninit_i32(a?: Int32Array): Int32Array | undefined;
70+
export function echo_option_vec_uninit_u64(a?: BigUint64Array): BigUint64Array | undefined;
71+
export function echo_option_vec_uninit_i64(a?: BigInt64Array): BigInt64Array | undefined;
5672
export function echo_option_vec_string(a?: (string)[]): (string)[] | undefined;
5773
export function echo_option_struct(a?: Foo): Foo | undefined;
5874
export function echo_option_vec_struct(a?: (Foo)[]): (Foo)[] | undefined;

crates/cli/tests/reference/echo.js

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,110 @@ export function echo_vec_i64(a) {
554554
return v2;
555555
}
556556

557+
/**
558+
* @param {Uint8Array} a
559+
* @returns {Uint8Array}
560+
*/
561+
export function echo_vec_uninit_u8(a) {
562+
const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc);
563+
const len0 = WASM_VECTOR_LEN;
564+
const ret = wasm.echo_vec_uninit_u8(ptr0, len0);
565+
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
566+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
567+
return v2;
568+
}
569+
570+
/**
571+
* @param {Int8Array} a
572+
* @returns {Int8Array}
573+
*/
574+
export function echo_vec_uninit_i8(a) {
575+
const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc);
576+
const len0 = WASM_VECTOR_LEN;
577+
const ret = wasm.echo_vec_uninit_i8(ptr0, len0);
578+
var v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice();
579+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
580+
return v2;
581+
}
582+
583+
/**
584+
* @param {Uint16Array} a
585+
* @returns {Uint16Array}
586+
*/
587+
export function echo_vec_uninit_u16(a) {
588+
const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc);
589+
const len0 = WASM_VECTOR_LEN;
590+
const ret = wasm.echo_vec_uninit_u16(ptr0, len0);
591+
var v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice();
592+
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
593+
return v2;
594+
}
595+
596+
/**
597+
* @param {Int16Array} a
598+
* @returns {Int16Array}
599+
*/
600+
export function echo_vec_uninit_i16(a) {
601+
const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc);
602+
const len0 = WASM_VECTOR_LEN;
603+
const ret = wasm.echo_vec_uninit_i16(ptr0, len0);
604+
var v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice();
605+
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
606+
return v2;
607+
}
608+
609+
/**
610+
* @param {Uint32Array} a
611+
* @returns {Uint32Array}
612+
*/
613+
export function echo_vec_uninit_u32(a) {
614+
const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc);
615+
const len0 = WASM_VECTOR_LEN;
616+
const ret = wasm.echo_vec_uninit_u32(ptr0, len0);
617+
var v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
618+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
619+
return v2;
620+
}
621+
622+
/**
623+
* @param {Int32Array} a
624+
* @returns {Int32Array}
625+
*/
626+
export function echo_vec_uninit_i32(a) {
627+
const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc);
628+
const len0 = WASM_VECTOR_LEN;
629+
const ret = wasm.echo_vec_uninit_i32(ptr0, len0);
630+
var v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice();
631+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
632+
return v2;
633+
}
634+
635+
/**
636+
* @param {BigUint64Array} a
637+
* @returns {BigUint64Array}
638+
*/
639+
export function echo_vec_uninit_u64(a) {
640+
const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc);
641+
const len0 = WASM_VECTOR_LEN;
642+
const ret = wasm.echo_vec_uninit_u64(ptr0, len0);
643+
var v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice();
644+
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
645+
return v2;
646+
}
647+
648+
/**
649+
* @param {BigInt64Array} a
650+
* @returns {BigInt64Array}
651+
*/
652+
export function echo_vec_uninit_i64(a) {
653+
const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc);
654+
const len0 = WASM_VECTOR_LEN;
655+
const ret = wasm.echo_vec_uninit_i64(ptr0, len0);
656+
var v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice();
657+
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
658+
return v2;
659+
}
660+
557661
function addToExternrefTable0(obj) {
558662
const idx = wasm.__externref_table_alloc();
559663
wasm.__wbindgen_export_2.set(idx, obj);
@@ -912,6 +1016,134 @@ export function echo_option_vec_i64(a) {
9121016
return v2;
9131017
}
9141018

1019+
/**
1020+
* @param {Uint8Array | undefined} [a]
1021+
* @returns {Uint8Array | undefined}
1022+
*/
1023+
export function echo_option_vec_uninit_u8(a) {
1024+
var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc);
1025+
var len0 = WASM_VECTOR_LEN;
1026+
const ret = wasm.echo_option_vec_uninit_u8(ptr0, len0);
1027+
let v2;
1028+
if (ret[0] !== 0) {
1029+
v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
1030+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
1031+
}
1032+
return v2;
1033+
}
1034+
1035+
/**
1036+
* @param {Int8Array | undefined} [a]
1037+
* @returns {Int8Array | undefined}
1038+
*/
1039+
export function echo_option_vec_uninit_i8(a) {
1040+
var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc);
1041+
var len0 = WASM_VECTOR_LEN;
1042+
const ret = wasm.echo_option_vec_uninit_i8(ptr0, len0);
1043+
let v2;
1044+
if (ret[0] !== 0) {
1045+
v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice();
1046+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
1047+
}
1048+
return v2;
1049+
}
1050+
1051+
/**
1052+
* @param {Uint16Array | undefined} [a]
1053+
* @returns {Uint16Array | undefined}
1054+
*/
1055+
export function echo_option_vec_uninit_u16(a) {
1056+
var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc);
1057+
var len0 = WASM_VECTOR_LEN;
1058+
const ret = wasm.echo_option_vec_uninit_u16(ptr0, len0);
1059+
let v2;
1060+
if (ret[0] !== 0) {
1061+
v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice();
1062+
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
1063+
}
1064+
return v2;
1065+
}
1066+
1067+
/**
1068+
* @param {Int16Array | undefined} [a]
1069+
* @returns {Int16Array | undefined}
1070+
*/
1071+
export function echo_option_vec_uninit_i16(a) {
1072+
var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc);
1073+
var len0 = WASM_VECTOR_LEN;
1074+
const ret = wasm.echo_option_vec_uninit_i16(ptr0, len0);
1075+
let v2;
1076+
if (ret[0] !== 0) {
1077+
v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice();
1078+
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
1079+
}
1080+
return v2;
1081+
}
1082+
1083+
/**
1084+
* @param {Uint32Array | undefined} [a]
1085+
* @returns {Uint32Array | undefined}
1086+
*/
1087+
export function echo_option_vec_uninit_u32(a) {
1088+
var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc);
1089+
var len0 = WASM_VECTOR_LEN;
1090+
const ret = wasm.echo_option_vec_uninit_u32(ptr0, len0);
1091+
let v2;
1092+
if (ret[0] !== 0) {
1093+
v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
1094+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
1095+
}
1096+
return v2;
1097+
}
1098+
1099+
/**
1100+
* @param {Int32Array | undefined} [a]
1101+
* @returns {Int32Array | undefined}
1102+
*/
1103+
export function echo_option_vec_uninit_i32(a) {
1104+
var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc);
1105+
var len0 = WASM_VECTOR_LEN;
1106+
const ret = wasm.echo_option_vec_uninit_i32(ptr0, len0);
1107+
let v2;
1108+
if (ret[0] !== 0) {
1109+
v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice();
1110+
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
1111+
}
1112+
return v2;
1113+
}
1114+
1115+
/**
1116+
* @param {BigUint64Array | undefined} [a]
1117+
* @returns {BigUint64Array | undefined}
1118+
*/
1119+
export function echo_option_vec_uninit_u64(a) {
1120+
var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc);
1121+
var len0 = WASM_VECTOR_LEN;
1122+
const ret = wasm.echo_option_vec_uninit_u64(ptr0, len0);
1123+
let v2;
1124+
if (ret[0] !== 0) {
1125+
v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice();
1126+
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
1127+
}
1128+
return v2;
1129+
}
1130+
1131+
/**
1132+
* @param {BigInt64Array | undefined} [a]
1133+
* @returns {BigInt64Array | undefined}
1134+
*/
1135+
export function echo_option_vec_uninit_i64(a) {
1136+
var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc);
1137+
var len0 = WASM_VECTOR_LEN;
1138+
const ret = wasm.echo_option_vec_uninit_i64(ptr0, len0);
1139+
let v2;
1140+
if (ret[0] !== 0) {
1141+
v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice();
1142+
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
1143+
}
1144+
return v2;
1145+
}
1146+
9151147
/**
9161148
* @param {(string)[] | undefined} [a]
9171149
* @returns {(string)[] | undefined}

crates/cli/tests/reference/echo.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::mem::MaybeUninit;
12
use wasm_bindgen::prelude::*;
23

34
#[wasm_bindgen]
@@ -42,6 +43,14 @@ echo!(
4243
(echo_vec_i32, Vec<i32>),
4344
(echo_vec_u64, Vec<u64>),
4445
(echo_vec_i64, Vec<i64>),
46+
(echo_vec_uninit_u8, Vec<MaybeUninit<u8>>),
47+
(echo_vec_uninit_i8, Vec<MaybeUninit<i8>>),
48+
(echo_vec_uninit_u16, Vec<MaybeUninit<u16>>),
49+
(echo_vec_uninit_i16, Vec<MaybeUninit<i16>>),
50+
(echo_vec_uninit_u32, Vec<MaybeUninit<u32>>),
51+
(echo_vec_uninit_i32, Vec<MaybeUninit<i32>>),
52+
(echo_vec_uninit_u64, Vec<MaybeUninit<u64>>),
53+
(echo_vec_uninit_i64, Vec<MaybeUninit<i64>>),
4554
(echo_vec_string, Vec<String>),
4655
(echo_struct, Foo),
4756
(echo_vec_struct, Vec<Foo>),
@@ -70,6 +79,14 @@ echo!(
7079
(echo_option_vec_i32, Option<Vec<i32>>),
7180
(echo_option_vec_u64, Option<Vec<u64>>),
7281
(echo_option_vec_i64, Option<Vec<i64>>),
82+
(echo_option_vec_uninit_u8, Option<Vec<MaybeUninit<u8>>>),
83+
(echo_option_vec_uninit_i8, Option<Vec<MaybeUninit<i8>>>),
84+
(echo_option_vec_uninit_u16, Option<Vec<MaybeUninit<u16>>>),
85+
(echo_option_vec_uninit_i16, Option<Vec<MaybeUninit<i16>>>),
86+
(echo_option_vec_uninit_u32, Option<Vec<MaybeUninit<u32>>>),
87+
(echo_option_vec_uninit_i32, Option<Vec<MaybeUninit<i32>>>),
88+
(echo_option_vec_uninit_u64, Option<Vec<MaybeUninit<u64>>>),
89+
(echo_option_vec_uninit_i64, Option<Vec<MaybeUninit<i64>>>),
7390
(echo_option_vec_string, Option<Vec<String>>),
7491
(echo_option_struct, Option<Foo>),
7592
(echo_option_vec_struct, Option<Vec<Foo>>)

0 commit comments

Comments
 (0)