napi/bindgen_runtime/js_values/
nil.rs1use std::ptr;
2
3use crate::{bindgen_prelude::*, check_status, sys, Result, 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 Ok(Null)
24 }
25}
26
27impl ToNapiValue for Null {
28 unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
29 let mut ret = ptr::null_mut();
30
31 check_status!(
32 unsafe { sys::napi_get_null(env, &mut ret) },
33 "Failed to create napi null value"
34 )?;
35
36 Ok(ret)
37 }
38}
39
40impl ToNapiValue for &Null {
41 unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
42 ToNapiValue::to_napi_value(env, *val)
43 }
44}
45
46impl ToNapiValue for &mut 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 TypeName for Undefined {
53 fn type_name() -> &'static str {
54 "undefined"
55 }
56
57 fn value_type() -> ValueType {
58 ValueType::Undefined
59 }
60}
61
62impl ValidateNapiValue for Undefined {}
63
64impl FromNapiValue for Undefined {
65 unsafe fn from_napi_value(_env: sys::napi_env, _napi_val: sys::napi_value) -> Result<Self> {
66 Ok(())
67 }
68}
69
70impl ToNapiValue for Undefined {
71 unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
72 let mut ret = ptr::null_mut();
73
74 check_status!(
75 unsafe { sys::napi_get_undefined(env, &mut ret) },
76 "Failed to create napi undefined value"
77 )?;
78
79 Ok(ret)
80 }
81}
82
83impl ToNapiValue for &Undefined {
84 unsafe fn to_napi_value(env: sys::napi_env, _: Self) -> Result<sys::napi_value> {
85 ToNapiValue::to_napi_value(env, ())
86 }
87}
88
89impl ToNapiValue for &mut Undefined {
90 unsafe fn to_napi_value(env: sys::napi_env, _: Self) -> Result<sys::napi_value> {
91 ToNapiValue::to_napi_value(env, ())
92 }
93}