Merge pull request #939 from napi-rs/fix/buffer-vec-conversion
fix(napi): impl From<Buffer> for Vec<u8>
This commit is contained in:
commit
3f2e44d3db
6 changed files with 20 additions and 1 deletions
|
@ -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 {
|
impl From<&[u8]> for Buffer {
|
||||||
fn from(inner: &[u8]) -> Self {
|
fn from(inner: &[u8]) -> Self {
|
||||||
Buffer::from(inner.to_owned())
|
Buffer::from(inner.to_owned())
|
||||||
|
|
|
@ -111,6 +111,7 @@ Generated by [AVA](https://avajs.dev).
|
||||||
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊
|
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊
|
||||||
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊
|
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊
|
||||||
export function getBuffer(): Buffer␊
|
export function getBuffer(): Buffer␊
|
||||||
|
export function appendBuffer(buf: Buffer): Buffer␊
|
||||||
export function convertU32Array(input: Uint32Array): Array<number>␊
|
export function convertU32Array(input: Uint32Array): Array<number>␊
|
||||||
export function createExternalTypedArray(): Uint32Array␊
|
export function createExternalTypedArray(): Uint32Array␊
|
||||||
export function mutateTypedArray(input: Float32Array): void␊
|
export function mutateTypedArray(input: Float32Array): void␊
|
||||||
|
|
Binary file not shown.
|
@ -64,6 +64,7 @@ import {
|
||||||
fnReceivedAliased,
|
fnReceivedAliased,
|
||||||
ALIAS,
|
ALIAS,
|
||||||
AliasedStruct,
|
AliasedStruct,
|
||||||
|
appendBuffer,
|
||||||
} from '../'
|
} from '../'
|
||||||
|
|
||||||
test('export const', (t) => {
|
test('export const', (t) => {
|
||||||
|
@ -239,7 +240,10 @@ test('serde-json', (t) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('buffer', (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) => {
|
test('convert typedarray to vec', (t) => {
|
||||||
|
|
1
examples/napi/index.d.ts
vendored
1
examples/napi/index.d.ts
vendored
|
@ -101,6 +101,7 @@ export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void
|
||||||
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void
|
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void
|
||||||
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void
|
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void
|
||||||
export function getBuffer(): Buffer
|
export function getBuffer(): Buffer
|
||||||
|
export function appendBuffer(buf: Buffer): Buffer
|
||||||
export function convertU32Array(input: Uint32Array): Array<number>
|
export function convertU32Array(input: Uint32Array): Array<number>
|
||||||
export function createExternalTypedArray(): Uint32Array
|
export function createExternalTypedArray(): Uint32Array
|
||||||
export function mutateTypedArray(input: Float32Array): void
|
export function mutateTypedArray(input: Float32Array): void
|
||||||
|
|
|
@ -5,6 +5,13 @@ fn get_buffer() -> Buffer {
|
||||||
String::from("Hello world").as_bytes().into()
|
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]
|
#[napi]
|
||||||
fn convert_u32_array(input: Uint32Array) -> Vec<u32> {
|
fn convert_u32_array(input: Uint32Array) -> Vec<u32> {
|
||||||
input.to_vec()
|
input.to_vec()
|
||||||
|
|
Loading…
Reference in a new issue