Skip to content

Commit

Permalink
Merge pull request #939 from napi-rs/fix/buffer-vec-conversion
Browse files Browse the repository at this point in the history
fix(napi): impl From<Buffer> for Vec<u8>
  • Loading branch information
Brooooooklyn committed Dec 18, 2021
2 parents a256129 + 2df97c1 commit 3f2e44d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/napi/src/bindgen_runtime/js_values/buffer.rs
Expand Up @@ -16,6 +16,12 @@ impl From<Vec<u8>> for Buffer {
}
}

impl From<Buffer> for Vec<u8> {
fn from(buf: Buffer) -> Self {
buf.inner.to_vec()
}
}

impl From<&[u8]> for Buffer {
fn from(inner: &[u8]) -> Self {
Buffer::from(inner.to_owned())
Expand Down
1 change: 1 addition & 0 deletions examples/napi/__test__/typegen.spec.ts.md
Expand Up @@ -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<number>␊
export function createExternalTypedArray(): Uint32Array␊
export function mutateTypedArray(input: Float32Array): void␊
Expand Down
Binary file modified examples/napi/__test__/typegen.spec.ts.snap
Binary file not shown.
6 changes: 5 additions & 1 deletion examples/napi/__test__/values.spec.ts
Expand Up @@ -64,6 +64,7 @@ import {
fnReceivedAliased,
ALIAS,
AliasedStruct,
appendBuffer,
} from '../'

test('export const', (t) => {
Expand Down Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions examples/napi/index.d.ts
Expand Up @@ -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<number>
export function createExternalTypedArray(): Uint32Array
export function mutateTypedArray(input: Float32Array): void
Expand Down
7 changes: 7 additions & 0 deletions examples/napi/src/typed_array.rs
Expand Up @@ -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::<u8>::from(buf);
buf.push('!' as u8);
buf.into()
}

#[napi]
fn convert_u32_array(input: Uint32Array) -> Vec<u32> {
input.to_vec()
Expand Down

0 comments on commit 3f2e44d

Please sign in to comment.