From 9aec0ec38e6e817acb292b42b0ea9c157f8af337 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 3 Sep 2020 20:38:28 +0800 Subject: [PATCH] docs: upgrade docs --- napi/Cargo.toml | 1 + napi/src/env.rs | 38 +++++++++++++++++++++++++++++++++----- napi/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/napi/Cargo.toml b/napi/Cargo.toml index 55c6d1d2..6dbb4c5f 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -48,3 +48,4 @@ napi-build = { version = "0.2", path = "../build" } [package.metadata.docs.rs] rustc-args = ["--cfg", "napidocsrs"] +all-features = true diff --git a/napi/src/env.rs b/napi/src/env.rs index 5dde9187..5220c4f1 100644 --- a/napi/src/env.rs +++ b/napi/src/env.rs @@ -99,7 +99,7 @@ impl Env { #[cfg(napi6)] #[inline] - /// https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_int64 + /// [n_api_napi_create_bigint_int64](https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_int64) pub fn create_bigint_from_i64(&self, value: i64) -> Result { let mut raw_value = ptr::null_mut(); check_status(unsafe { sys::napi_create_bigint_int64(self.0, value, &mut raw_value) })?; @@ -108,7 +108,6 @@ impl Env { #[cfg(napi6)] #[inline] - /// https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_words pub fn create_bigint_from_u64(&self, value: u64) -> Result { let mut raw_value = ptr::null_mut(); check_status(unsafe { sys::napi_create_bigint_uint64(self.0, value, &mut raw_value) })?; @@ -117,7 +116,6 @@ impl Env { #[cfg(napi6)] #[inline] - /// https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_words pub fn create_bigint_from_i128(&self, value: i128) -> Result { let mut raw_value = ptr::null_mut(); let sign_bit = if value > 0 { 0 } else { 1 }; @@ -130,7 +128,6 @@ impl Env { #[cfg(napi6)] #[inline] - /// https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_words pub fn create_bigint_from_u128(&self, value: u128) -> Result { let mut raw_value = ptr::null_mut(); let words = &value as *const u128 as *const u64; @@ -140,7 +137,7 @@ impl Env { #[cfg(napi6)] #[inline] - /// https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_words + /// [n_api_napi_create_bigint_words](https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_words) /// The resulting BigInt will be negative when sign_bit is true. pub fn create_bigint_from_words(&self, sign_bit: bool, words: Vec) -> Result { let mut raw_value = ptr::null_mut(); @@ -633,6 +630,21 @@ impl Env { Ok(JsObject::from_raw_unchecked(self.0, raw_promise)) } + /// # Serialize `Rust Struct` into `JavaScript Value` + /// ``` + /// #[derive(Serialize, Debug, Deserialize)] + /// struct AnObject { + /// a: u32, + /// b: Vec, + /// c: String, + /// } + /// + /// #[js_function] + /// fn serialize(ctx: CallContext) -> Result { + /// let value = AnyObject { a: 1, b: vec![0.1, 2.22], c: "hello" }; + /// ctx.env.to_js_value(&value) + /// } + /// ``` #[cfg(feature = "serde-json")] #[inline] pub fn to_js_value(&self, node: &T) -> Result @@ -643,6 +655,22 @@ impl Env { node.serialize(s).map(JsUnknown) } + /// # Deserialize data from `JsValue` + /// ``` + /// #[derive(Serialize, Debug, Deserialize)] + /// struct AnObject { + /// a: u32, + /// b: Vec, + /// c: String, + /// } + /// + /// #[js_function(1)] + /// fn deserialize_from_js(ctx: CallContext) -> Result { + /// let arg0 = ctx.get::(0)?; + /// let de_serialized: AnObject = ctx.env.from_js_value(arg0)?; + /// ... + /// } + /// #[cfg(feature = "serde-json")] #[inline] pub fn from_js_value(&self, value: V) -> Result diff --git a/napi/src/lib.rs b/napi/src/lib.rs index e2e5411c..a94e85ce 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -1,7 +1,9 @@ //! High level NodeJS [N-API](https://nodejs.org/api/n-api.html) binding //! //! **napi-rs** provides minimal overhead to write N-API modules in `Rust`. +//! //! ## Feature flags +//! //! ### libuv //! With `libuv` feature, you can execute a rust future in `libuv` in NodeJS, and return a `promise` object. //! ``` @@ -55,6 +57,38 @@ //! NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE=1000 node ./app.js //! ``` //! +//! ### latin1 +//! +//! Decode latin1 string from JavaScript using [encoding_rs](https://docs.rs/encoding_rs). +//! +//! With this feature, you can use `JsString.as_latin1_string` function +//! +//! ### serde-json +//! +//! Enable Serialize/Deserialize data cross `JavaScript Object` and `Rust struct`. +//! +//! ``` +//! #[derive(Serialize, Debug, Deserialize)] +//! struct AnObject { +//! a: u32, +//! b: Vec, +//! c: String, +//! } +//! +//! #[js_function(1)] +//! fn deserialize_from_js(ctx: CallContext) -> Result { +//! let arg0 = ctx.get::(0)?; +//! let de_serialized: AnObject = ctx.env.from_js_value(arg0)?; +//! ... +//! } +//! +//! #[js_function] +//! fn serialize(ctx: CallContext) -> Result { +//! let value = AnyObject { a: 1, b: vec![0.1, 2.22], c: "hello" }; +//! ctx.env.to_js_value(&value) +//! } +//! ``` +//! mod async_work; mod call_context;