From ea50a0c1301ae6f2255eb6b12b08b650aebd4c21 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 30 Apr 2020 18:57:04 +0800 Subject: [PATCH] docs: add taste codes in README --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index ddebdf48..ca32751f 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,23 @@ This library depends on N-API and requires Node 8.9 or later. It is still pretty One nice feature is that this crate allows you to build add-ons purely with the Rust toolchain and without involving `node-gyp`. +## Taste +```rs +#[js_function(1)] // ------> argument length, omit for zero +fn fibonacci<'env>(ctx: CallContext<'env>) -> Result> { + let n = ctx.get::(0)?.try_into()?; + ctx.env.create_int64(fibonacci_native(n)) +} + +#[inline] +fn fibonacci_native(n: i64) -> i64 { + match n { + 1 | 2 => 1, + _ => fibonacci_native(n - 1) + fibonacci_native(n - 2), + } +} +``` + ## Building This repository is a Cargo crate. Any napi-based add-on should contain `Cargo.toml` to make it a Cargo crate.