native buffer no need to keep raw napi pointer
This commit is contained in:
parent
698bc701e8
commit
99b2723618
5 changed files with 23 additions and 38 deletions
|
@ -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<sys::napi_value>,
|
||||
inner: mem::ManuallyDrop<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for Buffer {
|
||||
fn from(data: Vec<u8>) -> 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,9 +72,6 @@ impl FromNapiValue for Buffer {
|
|||
|
||||
impl ToNapiValue for Buffer {
|
||||
unsafe fn to_napi_value(env: sys::napi_env, mut val: Self) -> Result<sys::napi_value> {
|
||||
match val.raw {
|
||||
Some(raw) => Ok(raw),
|
||||
None => {
|
||||
let len = val.inner.len();
|
||||
let mut ret = ptr::null_mut();
|
||||
check_status!(
|
||||
|
@ -94,8 +88,6 @@ impl ToNapiValue for Buffer {
|
|||
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ValidateNapiValue for Buffer {
|
||||
|
@ -103,9 +95,3 @@ impl ValidateNapiValue for Buffer {
|
|||
vec![ValueType::Object]
|
||||
}
|
||||
}
|
||||
|
||||
impl ToNapiValue for Vec<u8> {
|
||||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||
Buffer::to_napi_value(env, val.into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
`export function getWords(): Array<string>␊
|
||||
export function getNums(): Array<number>␊
|
||||
export function sumNums(nums: Array<number>): number␊
|
||||
export function readFileAsync(path: string): Promise<Array<number>>␊
|
||||
export function readFileAsync(path: string): Promise<Buffer>␊
|
||||
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 }␊
|
||||
|
|
Binary file not shown.
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -1,7 +1,7 @@
|
|||
export function getWords(): Array<string>
|
||||
export function getNums(): Array<number>
|
||||
export function sumNums(nums: Array<number>): number
|
||||
export function readFileAsync(path: string): Promise<Array<number>>
|
||||
export function readFileAsync(path: string): Promise<Buffer>
|
||||
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 }
|
||||
|
|
|
@ -3,15 +3,14 @@ use napi::bindgen_prelude::*;
|
|||
use tokio::fs;
|
||||
|
||||
#[napi]
|
||||
async fn read_file_async(path: String) -> Result<Vec<u8>> {
|
||||
async fn read_file_async(path: String) -> Result<Buffer> {
|
||||
fs::read(path)
|
||||
.map(|v| {
|
||||
v.map_err(|e| {
|
||||
Error::new(
|
||||
.map(|r| match r {
|
||||
Ok(content) => Ok(content.into()),
|
||||
Err(e) => Err(Error::new(
|
||||
Status::GenericFailure,
|
||||
format!("failed to read file, {}", e),
|
||||
)
|
||||
})
|
||||
)),
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue