From 2df97c108f6cd16b97dd502eb49084a76dd0324f Mon Sep 17 00:00:00 2001 From: forehalo Date: Sat, 18 Dec 2021 13:24:16 +0800 Subject: [PATCH] fix: impl From for Vec --- .../src/bindgen_runtime/js_values/buffer.rs | 6 ++++++ examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 1958 -> 1972 bytes examples/napi/__test__/values.spec.ts | 6 +++++- examples/napi/index.d.ts | 1 + examples/napi/src/typed_array.rs | 7 +++++++ 6 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/napi/src/bindgen_runtime/js_values/buffer.rs b/crates/napi/src/bindgen_runtime/js_values/buffer.rs index 66fd324a..e585fc1a 100644 --- a/crates/napi/src/bindgen_runtime/js_values/buffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/buffer.rs @@ -16,6 +16,12 @@ impl From> for Buffer { } } +impl From for Vec { + fn from(buf: Buffer) -> Self { + buf.inner.to_vec() + } +} + impl From<&[u8]> for Buffer { fn from(inner: &[u8]) -> Self { Buffer::from(inner.to_owned()) diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 32cb0ed4..4424ebd3 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -111,6 +111,7 @@ Generated by [AVA](https://avajs.dev). export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊ export function getBuffer(): Buffer␊ + export function appendBuffer(buf: Buffer): Buffer␊ export function convertU32Array(input: Uint32Array): Array␊ export function createExternalTypedArray(): Uint32Array␊ export function mutateTypedArray(input: Float32Array): void␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 29418c0f92083bdbb54323db185f91c9ecb9fe9e..d392610085ed0b9a492f6c2b7aadf401768adf88 100644 GIT binary patch literal 1972 zcmV;l2TS-t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<0 zC~^wugETQ9JQ)Hh)&b&)N4ifAaI_4(-R9w(->KW5K01;oRE)<8J7Yl7rf%Ej>+z2s z$8gxTB;!o)$_S=-Pmg2sHi8LfYpP;2X9Q6KMFto~Bw_I0V=>Makr1kv0So5z2xNIpWpmo_tCu5 zBSjNFrPjZbvmU@dbs4Z0<~DnsX6{orgzY8j7`r6Hpcg@OVB-ellBLulW;TKwf*!Nw zjW4MNT-ZSR{$iuxn2p*wRS4hB_B6|+7*5FpN@&+-&M;lVTwt{%bQH2KWAVMVH~v;s zX&(s9Y4h$wnh0X}$15+n4%A>V;5qiZ3cJrxj+?PJJq%VfCk{7xJ2q{XPuZFxY|d)k z#D7ugb`I4FP2Lr|CG@pRA{`m>iVwO^wz@sCKsj&?e~`7+$j^WqX^j5zLW2-wKGMv{M(^w|`P s=P@6^n+bdq&TD+P?EB8Y&@#5tm&|+BK7D@aym~(OKV+&`IJy=9010ug&j0`b diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 7a98e09a..daefa0ca 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -64,6 +64,7 @@ import { fnReceivedAliased, ALIAS, AliasedStruct, + appendBuffer, } from '../' test('export const', (t) => { @@ -239,7 +240,10 @@ test('serde-json', (t) => { }) test('buffer', (t) => { - t.is(getBuffer().toString('utf-8'), 'Hello world') + let buf = getBuffer() + t.is(buf.toString('utf-8'), 'Hello world') + buf = appendBuffer(buf) + t.is(buf.toString('utf-8'), 'Hello world!') }) test('convert typedarray to vec', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 3f9cdc46..ab36c034 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -101,6 +101,7 @@ export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void export function getBuffer(): Buffer +export function appendBuffer(buf: Buffer): Buffer export function convertU32Array(input: Uint32Array): Array export function createExternalTypedArray(): Uint32Array export function mutateTypedArray(input: Float32Array): void diff --git a/examples/napi/src/typed_array.rs b/examples/napi/src/typed_array.rs index 5d85194b..ba779e9a 100644 --- a/examples/napi/src/typed_array.rs +++ b/examples/napi/src/typed_array.rs @@ -5,6 +5,13 @@ fn get_buffer() -> Buffer { String::from("Hello world").as_bytes().into() } +#[napi] +fn append_buffer(buf: Buffer) -> Buffer { + let mut buf = Vec::::from(buf); + buf.push('!' as u8); + buf.into() +} + #[napi] fn convert_u32_array(input: Uint32Array) -> Vec { input.to_vec()