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
examples/napi
|
@ -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(
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue