diff --git a/crates/napi/src/bindgen_runtime/js_values/buffer.rs b/crates/napi/src/bindgen_runtime/js_values/buffer.rs index ebe3382c..81ae87a0 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 29418c0f..d3926100 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ 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()