diff --git a/crates/napi/src/bindgen_runtime/js_values/buffer.rs b/crates/napi/src/bindgen_runtime/js_values/buffer.rs index 697d4bbd..64024d8a 100644 --- a/crates/napi/src/bindgen_runtime/js_values/buffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/buffer.rs @@ -5,14 +5,12 @@ use crate::{bindgen_prelude::*, check_status, sys, Result, ValueType}; /// zero copy u8 vector shared between rust and napi pub struct Buffer { - raw: Option, inner: mem::ManuallyDrop>, } impl From> for Buffer { fn from(data: Vec) -> Self { Buffer { - raw: None, inner: mem::ManuallyDrop::new(data), } } @@ -67,7 +65,6 @@ impl FromNapiValue for Buffer { )?; Ok(Self { - raw: Some(napi_val), inner: mem::ManuallyDrop::new(Vec::from_raw_parts(buf as *mut _, len, len)), }) } @@ -75,26 +72,21 @@ impl FromNapiValue for Buffer { impl ToNapiValue for Buffer { unsafe fn to_napi_value(env: sys::napi_env, mut val: Self) -> Result { - match val.raw { - Some(raw) => Ok(raw), - None => { - let len = val.inner.len(); - let mut ret = ptr::null_mut(); - check_status!( - sys::napi_create_external_buffer( - env, - len, - val.inner.as_mut_ptr() as *mut _, - Some(drop_buffer), - Box::into_raw(Box::new((len, val.inner.capacity()))) as *mut _, - &mut ret, - ), - "Failed to create napi buffer" - )?; + let len = val.inner.len(); + let mut ret = ptr::null_mut(); + check_status!( + sys::napi_create_external_buffer( + env, + len, + val.inner.as_mut_ptr() as *mut _, + Some(drop_buffer), + Box::into_raw(Box::new((len, val.inner.capacity()))) as *mut _, + &mut ret, + ), + "Failed to create napi buffer" + )?; - Ok(ret) - } - } + Ok(ret) } } @@ -103,9 +95,3 @@ impl ValidateNapiValue for Buffer { vec![ValueType::Object] } } - -impl ToNapiValue for Vec { - unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { - Buffer::to_napi_value(env, val.into()) - } -} diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 1c4cc031..4f4a206f 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -11,7 +11,7 @@ Generated by [AVA](https://avajs.dev). `export function getWords(): Array␊ export function getNums(): Array␊ export function sumNums(nums: Array): number␊ - export function readFileAsync(path: string): Promise>␊ + export function readFileAsync(path: string): Promise␊ export function getCwd(callback: (arg0: string) => void): void␊ export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊ export enum Kind { Dog = 0, Cat = 1, Duck = 2 }␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 3c0ac156..d423c065 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/index.d.ts b/examples/napi/index.d.ts index a6331520..d251a91d 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -1,7 +1,7 @@ export function getWords(): Array export function getNums(): Array export function sumNums(nums: Array): number -export function readFileAsync(path: string): Promise> +export function readFileAsync(path: string): Promise export function getCwd(callback: (arg0: string) => void): void export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void export enum Kind { Dog = 0, Cat = 1, Duck = 2 } diff --git a/examples/napi/src/async.rs b/examples/napi/src/async.rs index 7822cf7b..778c53ce 100644 --- a/examples/napi/src/async.rs +++ b/examples/napi/src/async.rs @@ -3,15 +3,14 @@ use napi::bindgen_prelude::*; use tokio::fs; #[napi] -async fn read_file_async(path: String) -> Result> { +async fn read_file_async(path: String) -> Result { fs::read(path) - .map(|v| { - v.map_err(|e| { - Error::new( - Status::GenericFailure, - format!("failed to read file, {}", e), - ) - }) + .map(|r| match r { + Ok(content) => Ok(content.into()), + Err(e) => Err(Error::new( + Status::GenericFailure, + format!("failed to read file, {}", e), + )), }) .await }