fix: impl From<Buffer> for Vec<u8>

This commit is contained in:
forehalo 2021-12-18 13:24:16 +08:00
parent 01b5a6dc3f
commit 2df97c108f
No known key found for this signature in database
GPG key ID: 64382C5AF49F3EB9
6 changed files with 20 additions and 1 deletions

View file

@ -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())

View file

@ -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␊

View file

@ -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) => {

View file

@ -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

View file

@ -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()