diff --git a/crates/napi/src/bindgen_runtime/js_values/either.rs b/crates/napi/src/bindgen_runtime/js_values/either.rs index 192aa142..2574b10a 100644 --- a/crates/napi/src/bindgen_runtime/js_values/either.rs +++ b/crates/napi/src/bindgen_runtime/js_values/either.rs @@ -1,5 +1,5 @@ use super::{FromNapiValue, ToNapiValue, TypeName}; -use crate::{type_of, Status, ValueType}; +use crate::{type_of, JsNull, JsUndefined, NapiRaw, Status, ValueType}; const ERROR_MSG: &str = "The return value of typeof(T) should not be equal in Either"; @@ -12,6 +12,19 @@ pub enum Either< B(B), } +impl< + A: TypeName + FromNapiValue + ToNapiValue + NapiRaw, + B: TypeName + FromNapiValue + ToNapiValue + NapiRaw, + > Either +{ + pub unsafe fn raw(&self) -> napi_sys::napi_value { + match &self { + Self::A(a) => a.raw(), + Self::B(b) => b.raw(), + } + } +} + impl TypeName for Either { @@ -24,6 +37,25 @@ impl From> for Option { + fn from(value: Either) -> Option { + match value { + Either::A(v) => Some(v), + Either::B(_) => None, + } + } +} + +impl From> for Option { + fn from(value: Either) -> Option { + match value { + Either::A(v) => Some(v), + Either::B(_) => None, + } + } +} + impl FromNapiValue for Either { diff --git a/crates/napi/src/call_context.rs b/crates/napi/src/call_context.rs index 7d846546..5f6bfa05 100644 --- a/crates/napi/src/call_context.rs +++ b/crates/napi/src/call_context.rs @@ -1,5 +1,6 @@ use std::ptr; +use crate::bindgen_runtime::{FromNapiValue, TypeName}; use crate::check_status; use crate::{sys, Either, Env, Error, JsUndefined, NapiValue, Result, Status}; @@ -45,18 +46,21 @@ impl<'env> CallContext<'env> { } } - pub fn get(&self, index: usize) -> Result { + pub fn get(&self, index: usize) -> Result { if index >= self.arg_len() { Err(Error::new( Status::GenericFailure, "Arguments index out of range".to_owned(), )) } else { - Ok(unsafe { ArgType::from_raw_unchecked(self.env.0, self.args[index]) }) + unsafe { ArgType::from_napi_value(self.env.0, self.args[index]) } } } - pub fn try_get(&self, index: usize) -> Result> { + pub fn try_get( + &self, + index: usize, + ) -> Result> { if index >= self.arg_len() { Err(Error::new( Status::GenericFailure, diff --git a/crates/napi/src/js_values/either.rs b/crates/napi/src/js_values/either.rs index 2fed39b8..c57fa403 100644 --- a/crates/napi/src/js_values/either.rs +++ b/crates/napi/src/js_values/either.rs @@ -1,37 +1 @@ -use crate::{sys, JsUndefined, NapiRaw, NapiValue, Result}; - -#[derive(Debug, Clone, Copy)] -pub enum Either { - A(A), - B(B), -} - -impl From> for Option { - fn from(value: Either) -> Option { - match value { - Either::A(v) => Some(v), - Either::B(_) => None, - } - } -} - -impl NapiValue for Either { - unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result> { - A::from_raw(env, value) - .map(Self::A) - .or_else(|_| B::from_raw(env, value).map(Self::B)) - } - - unsafe fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Either { - Self::from_raw(env, value).unwrap() - } -} - -impl NapiRaw for Either { - unsafe fn raw(&self) -> sys::napi_value { - match self { - Either::A(v) => v.raw(), - Either::B(v) => v.raw(), - } - } -} +pub use crate::bindgen_runtime::Either;