refactor(napi): remove compatible Either struct
Reexport `Either` from `bindgen_runtime::Either`
This commit is contained in:
parent
71d3c87700
commit
1dcd0fec1e
3 changed files with 41 additions and 41 deletions
|
@ -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<A, B>
|
||||
{
|
||||
pub unsafe fn raw(&self) -> napi_sys::napi_value {
|
||||
match &self {
|
||||
Self::A(a) => a.raw(),
|
||||
Self::B(b) => b.raw(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: TypeName + FromNapiValue + ToNapiValue, B: TypeName + FromNapiValue + ToNapiValue> TypeName
|
||||
for Either<A, B>
|
||||
{
|
||||
|
@ -24,6 +37,25 @@ impl<A: TypeName + FromNapiValue + ToNapiValue, B: TypeName + FromNapiValue + To
|
|||
}
|
||||
}
|
||||
|
||||
// Backwards compatibility with v1
|
||||
impl<T: TypeName + FromNapiValue + ToNapiValue> From<Either<T, JsUndefined>> for Option<T> {
|
||||
fn from(value: Either<T, JsUndefined>) -> Option<T> {
|
||||
match value {
|
||||
Either::A(v) => Some(v),
|
||||
Either::B(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: TypeName + FromNapiValue + ToNapiValue> From<Either<T, JsNull>> for Option<T> {
|
||||
fn from(value: Either<T, JsNull>) -> Option<T> {
|
||||
match value {
|
||||
Either::A(v) => Some(v),
|
||||
Either::B(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: TypeName + FromNapiValue + ToNapiValue, B: TypeName + FromNapiValue + ToNapiValue>
|
||||
FromNapiValue for Either<A, B>
|
||||
{
|
||||
|
|
|
@ -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<ArgType: NapiValue>(&self, index: usize) -> Result<ArgType> {
|
||||
pub fn get<ArgType: FromNapiValue>(&self, index: usize) -> Result<ArgType> {
|
||||
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<ArgType: NapiValue>(&self, index: usize) -> Result<Either<ArgType, JsUndefined>> {
|
||||
pub fn try_get<ArgType: NapiValue + TypeName + FromNapiValue>(
|
||||
&self,
|
||||
index: usize,
|
||||
) -> Result<Either<ArgType, JsUndefined>> {
|
||||
if index >= self.arg_len() {
|
||||
Err(Error::new(
|
||||
Status::GenericFailure,
|
||||
|
|
|
@ -1,37 +1 @@
|
|||
use crate::{sys, JsUndefined, NapiRaw, NapiValue, Result};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Either<A: NapiValue, B: NapiValue> {
|
||||
A(A),
|
||||
B(B),
|
||||
}
|
||||
|
||||
impl<T: NapiValue> From<Either<T, JsUndefined>> for Option<T> {
|
||||
fn from(value: Either<T, JsUndefined>) -> Option<T> {
|
||||
match value {
|
||||
Either::A(v) => Some(v),
|
||||
Either::B(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: NapiValue, B: NapiValue> NapiValue for Either<A, B> {
|
||||
unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result<Either<A, B>> {
|
||||
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<A, B> {
|
||||
Self::from_raw(env, value).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: NapiValue, B: NapiValue> NapiRaw for Either<A, B> {
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue