napi-rs/crates/napi/src/bindgen_runtime/js_values/boolean.rs
forehalo 2467b7139b
Introduce #[napi] procedural macro to automation development boilerplate (#696)
* napi procedural macro for basic rust/JavaScript types
* introduce the `compat-mode` for `napi` and `napi-derive` crates for backward compatible
* remove #[inline] and let compiler to decide the inline behavior
* cli now can produce the `.d.ts` file for native binding
* many tests and example for the new procedural macro

Co-authored-by: LongYinan <lynweklm@gmail.com>
2021-09-23 01:29:09 +08:00

39 lines
870 B
Rust

use crate::{bindgen_prelude::*, check_status, sys, ValueType};
impl TypeName for bool {
fn type_name() -> &'static str {
"bool"
}
}
impl ValidateNapiValue for bool {
fn type_of() -> Vec<ValueType> {
vec![ValueType::Boolean]
}
}
impl ToNapiValue for bool {
unsafe fn to_napi_value(env: sys::napi_env, val: bool) -> Result<sys::napi_value> {
let mut ptr = std::ptr::null_mut();
check_status!(
sys::napi_get_boolean(env, val, &mut ptr),
"Failed to convert rust type `bool` into napi value",
)?;
Ok(ptr)
}
}
impl FromNapiValue for bool {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
let mut ret = false;
check_status!(
sys::napi_get_value_bool(env, napi_val, &mut ret),
"Failed to convert napi value into rust type `bool`",
)?;
Ok(ret)
}
}