From 8b4e7af67f5a30d1a8815eb33f5398f53aef80f1 Mon Sep 17 00:00:00 2001 From: forehalo Date: Fri, 8 Oct 2021 21:13:27 +0800 Subject: [PATCH] buffer example --- .../src/bindgen_runtime/js_values/buffer.rs | 6 ++++++ examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 666 -> 676 bytes examples/napi/__test__/values.spec.ts | 5 +++++ examples/napi/index.d.ts | 1 + examples/napi/src/lib.rs | 1 + examples/napi/src/typed_array.rs | 6 ++++++ 7 files changed, 20 insertions(+) create mode 100644 examples/napi/src/typed_array.rs diff --git a/crates/napi/src/bindgen_runtime/js_values/buffer.rs b/crates/napi/src/bindgen_runtime/js_values/buffer.rs index b6f002f2..6b0c54cd 100644 --- a/crates/napi/src/bindgen_runtime/js_values/buffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/buffer.rs @@ -18,6 +18,12 @@ impl From> for Buffer { } } +impl From<&[u8]> for Buffer { + fn from(inner: &[u8]) -> Self { + Buffer::from(inner.to_owned()) + } +} + impl AsRef<[u8]> for Buffer { fn as_ref(&self) -> &[u8] { self.inner.as_slice() diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index c4386c50..b08ac600 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -34,6 +34,7 @@ Generated by [AVA](https://avajs.dev). export function concatStr(mutS: string): string␊ export function concatUtf16(s: string): string␊ export function concatLatin1(s: string): string␊ + export function getBuffer(): Buffer␊ export class Animal {␊ readonly kind: Kind␊ constructor(kind: Kind, name: string)␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 6a4ff45d71e3fe29dfe0ee49068193a15401e48a..cc5d8229b71a208443a42d8f34393e805338ed9f 100644 GIT binary patch literal 676 zcmV;V0$cq-RzVBv@}X&=fJG~nHkC?IgJ==rxL%KwZSCD?cbzmUeGOg#9)eqi z#2L=K0!PlwIyiOeMr{tx{CxB4Zwx^Yw1Sn-kAMC6^YPo4r_cWW{(kkf75LxJ)gV|2 z9+q-zHF)&kY8B5%+BirHC7jd>2IxL$lUUY?V8<9feQBMMYOvkD1$k8DHJ~c;7)=vs zi@XGq-`>Dd>`b~X`|c!Rf@fLG#aRT5o55NHvu--DwGCq}6H4I!8sP?cvM)1iCg>TX z4Sa$^C74QuNeJZVR+-78iY!}J0I6L#lq!J>*wX{pg0&EKxg(()!d@ZHNLq)>1^jMd zo%TEEdF=xF3TOC}iS}?3h6cTDBZPfj7?PfZa4gSB*bJd32SZ=sc?d_cjD0~NsC<&O z>ss^SebEzru(8fCS}eYN-)?C1q}&Z#w$q%C`Xeu~G0&<*)xK`%jbNT6j8~u#;!0fJ zQ7U7txDb-5yTO^Xt{M#;&w-Tq?^fQB=$q5rU%wLN~w72x6@>8gs`8B3z9bAY6-uW zTc`aF4!kyn6NNMU*;sox2?K-Pwi&{q&J9VALO7L|By5FnAo~Mf;b{n6S;Ri05L7XHwnyJZ7xgHN4?cWt$|HPi(mF|4^ z74@cTs<^u}1{*H)rP%g-oZ7Cma$G9QEKyX<7ZHM^141{y%?M(xQ{?qFD1+ec)ESoL z?z9rHx+3AiKX{RHYhhQ(jHktC^~=;MoxlYhc2t}jv5q { @@ -112,3 +113,7 @@ test('serde-json', (t) => { t.is(getPackageJsonName(packageJson), 'napi-rs') }) + +test('buffer', (t) => { + t.is(getBuffer().toString('utf-8'), 'Hello world') +}) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 0fcfc6d5..e7104db4 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -24,6 +24,7 @@ export function contains(source: string, target: string): boolean export function concatStr(mutS: string): string export function concatUtf16(s: string): string export function concatLatin1(s: string): string +export function getBuffer(): Buffer export class Animal { readonly kind: Kind constructor(kind: Kind, name: string) diff --git a/examples/napi/src/lib.rs b/examples/napi/src/lib.rs index e4d66c0c..e1a3699b 100644 --- a/examples/napi/src/lib.rs +++ b/examples/napi/src/lib.rs @@ -13,3 +13,4 @@ mod number; mod object; mod serde; mod string; +mod typed_array; diff --git a/examples/napi/src/typed_array.rs b/examples/napi/src/typed_array.rs new file mode 100644 index 00000000..8804c141 --- /dev/null +++ b/examples/napi/src/typed_array.rs @@ -0,0 +1,6 @@ +use napi::bindgen_prelude::*; + +#[napi] +fn get_buffer() -> Buffer { + String::from("Hello world").as_bytes().into() +}