diff --git a/crates/napi/README.md b/crates/napi/README.md index 977aa3a9..d6f8ec20 100644 --- a/crates/napi/README.md +++ b/crates/napi/README.md @@ -57,13 +57,12 @@ One nice feature is that this crate allows you to build add-ons purely with the ### Define JavaScript functions ```rust -/// import the preludes use napi::bindgen_prelude::*; use napi_derive::napi; /// module registration is done by the runtime, no need to explicitly do it now. #[napi] -fn fibonacci(n: u32) -> u32 { +pub fn fibonacci(n: u32) -> u32 { match n { 1 | 2 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), @@ -73,30 +72,31 @@ fn fibonacci(n: u32) -> u32 { /// use `Fn`, `FnMut` or `FnOnce` traits to defined JavaScript callbacks /// the return type of callbacks can only be `Result`. #[napi] -fn get_cwd Result<()>>(callback: T) { - callback(env::current_dir().unwrap().to_string_lossy().to_string()).unwrap(); +pub fn get_cwd Result<()>>(callback: T) { + callback( + std::env::current_dir() + .unwrap() + .to_string_lossy() + .to_string(), + ) + .unwrap(); } /// or, define the callback signature in where clause #[napi] -fn test_callback(callback: T) -where T: Fn(String) -> Result<()> -{} +pub fn test_callback(callback: T) -> Result<()> +where + T: Fn(String) -> Result<()>, +{ + callback(std::env::current_dir()?.to_string_lossy().to_string()) +} /// async fn, require `async` feature enabled. /// [dependencies] /// napi = {version="2", features=["async"]} #[napi] -async fn read_file_async(path: String) -> Result { - tokio::fs::read(path) - .map(|r| match r { - Ok(content) => Ok(content.into()), - Err(e) => Err(Error::new( - Status::GenericFailure, - format!("failed to read file, {}", e), - )), - }) - .await +pub async fn read_file_async(path: String) -> Result { + Ok(tokio::fs::read(path).await?.into()) } ```