napi/bindgen_runtime/js_values/
nil.rs

1use std::ptr;
2
3use crate::{bindgen_prelude::*, check_status, sys, type_of, Error, Result, Status, ValueType};
4
5#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
6pub struct Null;
7pub type Undefined = ();
8
9impl TypeName for Null {
10  fn type_name() -> &'static str {
11    "null"
12  }
13
14  fn value_type() -> ValueType {
15    ValueType::Null
16  }
17}
18
19impl ValidateNapiValue for Null {}
20
21impl FromNapiValue for Null {
22  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
23    match type_of!(env, napi_val) {
24      Ok(ValueType::Null) => Ok(Null),
25      _ => Err(Error::new(
26        Status::InvalidArg,
27        "Value is not null".to_owned(),
28      )),
29    }
30  }
31}
32
33impl ToNapiValue for Null {
34  unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
35    let mut ret = ptr::null_mut();
36
37    check_status!(
38      unsafe { sys::napi_get_null(env, &mut ret) },
39      "Failed to create napi null value"
40    )?;
41
42    Ok(ret)
43  }
44}
45
46impl ToNapiValue for &Null {
47  unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
48    ToNapiValue::to_napi_value(env, *val)
49  }
50}
51
52impl ToNapiValue for &mut Null {
53  unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
54    ToNapiValue::to_napi_value(env, *val)
55  }
56}
57
58impl TypeName for Undefined {
59  fn type_name() -> &'static str {
60    "undefined"
61  }
62
63  fn value_type() -> ValueType {
64    ValueType::Undefined
65  }
66}
67
68impl ValidateNapiValue for Undefined {}
69
70impl FromNapiValue for Undefined {
71  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
72    match type_of!(env, napi_val) {
73      Ok(ValueType::Undefined) => Ok(()),
74      _ => Err(Error::new(
75        Status::InvalidArg,
76        "Value is not undefined".to_owned(),
77      )),
78    }
79  }
80}
81
82impl ToNapiValue for Undefined {
83  unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
84    let mut ret = ptr::null_mut();
85
86    check_status!(
87      unsafe { sys::napi_get_undefined(env, &mut ret) },
88      "Failed to create napi undefined value"
89    )?;
90
91    Ok(ret)
92  }
93}
94
95impl ToNapiValue for &Undefined {
96  unsafe fn to_napi_value(env: sys::napi_env, _: Self) -> Result<sys::napi_value> {
97    ToNapiValue::to_napi_value(env, ())
98  }
99}
100
101impl ToNapiValue for &mut Undefined {
102  unsafe fn to_napi_value(env: sys::napi_env, _: Self) -> Result<sys::napi_value> {
103    ToNapiValue::to_napi_value(env, ())
104  }
105}