native buffer no need to keep raw napi pointer

This commit is contained in:
forehalo 2021-10-30 00:05:05 +08:00
parent 698bc701e8
commit 99b2723618
No known key found for this signature in database
GPG key ID: 64382C5AF49F3EB9
5 changed files with 23 additions and 38 deletions

View file

@ -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 }␊

View file

@ -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 }

View file

@ -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(
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
}