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 6a4ff45d..cc5d8229 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index f8dfdef9..ed0823e4 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -22,6 +22,7 @@ import { throwError, readPackageJson, getPackageJsonName, + getBuffer, } from '../' test('number', (t) => { @@ -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() +}