From 99b2723618d07321b85fdbd7827c772f890ae3a7 Mon Sep 17 00:00:00 2001 From: forehalo Date: Sat, 30 Oct 2021 00:05:05 +0800 Subject: [PATCH] native buffer no need to keep raw napi pointer --- .../src/bindgen_runtime/js_values/buffer.rs | 42 ++++++------------ examples/napi/__test__/typegen.spec.ts.md | 2 +- examples/napi/__test__/typegen.spec.ts.snap | Bin 698 -> 700 bytes examples/napi/index.d.ts | 2 +- examples/napi/src/async.rs | 15 +++---- 5 files changed, 23 insertions(+), 38 deletions(-) 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 3c0ac156fbbf503b2e64c160da2e54891ebb6c7a..d423c0652f5f2dd9568b6a563447a98af1e0cc69 100644 GIT binary patch literal 700 zcmV;t0z>^lRzVfJl>Zn?%JsmY{cE54YN$h=fvntTHJn}C&_J$lv;NZGr`j| z;$qka#!Y{H9sqVaFxE1rNd9jrcRj$KG1|Z<$W@GqR2YXqhE^GwEGkdaWdumI!l6_# zTtZLxVF%Vj=yFFwD}-Jyh9qsk)dIeoTc>>k_q=uq#|mfovx)X_5(WnS*=7j)IyWRe z3*l5=kgyfPp6m}i!^;qkWa0aYTu}KS>(^ZK;yvk^KG@t~*j^-Gepzl{^rYwwTh`Nz zkB&!PVq>0`fy#c}&}+^-ju|gOAw;FPJflQLT5%yHQ+IX}bNDu*WF0^x)PhxhS~CFErTK=BOAK_PXF#!`^VNsJ?;xl%$R?a4b7 zw1@w>Y0T8LQ?|!L$G!Wa>sRdOyON#HzM|f?O%?MIl|lPB*B5-7^Kxpv(#mnEEVDGC zV%~@l94!#K%5q0qr^xGRPz1rSd}DYq7OmCSfrT)l6&R_PRmwEXSj37}JAq}aJ|v^Lj}PJ00000000Ax zRLySFKoEARXrc^09m z18traK(gCASn{1nH7A2SK9VW!*|8FAz+JBmlT66x%;-#7+?nB;o8kT_W`d__#6`ai zjGOMplm~V@Fw`<8LjPHqLY?5y7;WGa9g;$pw`s*|?20E#3#c(66^P8MbGGFTOL^GkR3?hArx8#s{YZ zFR>v{OQN!0cl4Swk7LG5PzX^eF3u>Ckycy?$<+PeR9bf$T^{4uG96vEfnF%wy^A1d zP#lt{uX^IskjkM+xIj3g!QoweV+ncP08l)`a!^PeqOn9$If-$AG*?PUq;+|Rg4XeW zY#K8)?v(BE(BZ+d==v4=`L1N=v#+T0wy9!1p)zPKr}~UlArC4Pp@7ojb#_+?`h(7DpufdzZAfnOzqCmOH6h%it;LS9y|PG4d0s z#E6tz3wugtJT0!?H(aZ941HSucJT|KQ;JC3xp1^Lw}x 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 }