buffer example

This commit is contained in:
forehalo 2021-10-08 21:13:27 +08:00 committed by LongYinan
parent aa77c8ff97
commit 8b4e7af67f
7 changed files with 20 additions and 0 deletions

View file

@ -18,6 +18,12 @@ impl From<Vec<u8>> for Buffer {
} }
} }
impl From<&[u8]> for Buffer {
fn from(inner: &[u8]) -> Self {
Buffer::from(inner.to_owned())
}
}
impl AsRef<[u8]> for Buffer { impl AsRef<[u8]> for Buffer {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
self.inner.as_slice() self.inner.as_slice()

View file

@ -34,6 +34,7 @@ Generated by [AVA](https://avajs.dev).
export function concatStr(mutS: string): string␊ export function concatStr(mutS: string): string␊
export function concatUtf16(s: string): string␊ export function concatUtf16(s: string): string␊
export function concatLatin1(s: string): string␊ export function concatLatin1(s: string): string␊
export function getBuffer(): Buffer␊
export class Animal {␊ export class Animal {␊
readonly kind: Kind␊ readonly kind: Kind␊
constructor(kind: Kind, name: string)␊ constructor(kind: Kind, name: string)␊

View file

@ -22,6 +22,7 @@ import {
throwError, throwError,
readPackageJson, readPackageJson,
getPackageJsonName, getPackageJsonName,
getBuffer,
} from '../' } from '../'
test('number', (t) => { test('number', (t) => {
@ -112,3 +113,7 @@ test('serde-json', (t) => {
t.is(getPackageJsonName(packageJson), 'napi-rs') t.is(getPackageJsonName(packageJson), 'napi-rs')
}) })
test('buffer', (t) => {
t.is(getBuffer().toString('utf-8'), 'Hello world')
})

View file

@ -24,6 +24,7 @@ export function contains(source: string, target: string): boolean
export function concatStr(mutS: string): string export function concatStr(mutS: string): string
export function concatUtf16(s: string): string export function concatUtf16(s: string): string
export function concatLatin1(s: string): string export function concatLatin1(s: string): string
export function getBuffer(): Buffer
export class Animal { export class Animal {
readonly kind: Kind readonly kind: Kind
constructor(kind: Kind, name: string) constructor(kind: Kind, name: string)

View file

@ -13,3 +13,4 @@ mod number;
mod object; mod object;
mod serde; mod serde;
mod string; mod string;
mod typed_array;

View file

@ -0,0 +1,6 @@
use napi::bindgen_prelude::*;
#[napi]
fn get_buffer() -> Buffer {
String::from("Hello world").as_bytes().into()
}