feat(napi): add support for Vec<(std::string::String, u16)> and some other small change (#1320)
This commit is contained in:
parent
0d49b45ea9
commit
e54c37a0b1
4 changed files with 321 additions and 4 deletions
|
@ -302,3 +302,300 @@ where
|
|||
Ok(ptr::null_mut())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! arr_get {
|
||||
($arr:expr, $n:expr) => {
|
||||
if let Some(e) = $arr.get($n)? {
|
||||
e
|
||||
} else {
|
||||
return Err(Error::new(
|
||||
Status::InvalidArg,
|
||||
format!(
|
||||
"Found inconsistent data type in Array[{}] when converting to Rust T",
|
||||
$n
|
||||
)
|
||||
.to_owned(),
|
||||
));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! tuple_from_napi_value {
|
||||
($total:expr, $($n:expr),+) => {
|
||||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
||||
let arr = unsafe { Array::from_napi_value(env, napi_val)? };
|
||||
if arr.len() < $total {
|
||||
return Err(Error::new(
|
||||
Status::InvalidArg,
|
||||
format!("Array length < {}",$total).to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(($(arr_get!(arr,$n)),+))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1> FromNapiValue for (T0, T1)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(2, 0, 1);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2> FromNapiValue for (T0, T1, T2)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(3, 0, 1, 2);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3> FromNapiValue for (T0, T1, T2, T3)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(4, 0, 1, 2, 3);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4> FromNapiValue for (T0, T1, T2, T3, T4)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(5, 0, 1, 2, 3, 4);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5> FromNapiValue for (T0, T1, T2, T3, T4, T5)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(6, 0, 1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6> FromNapiValue for (T0, T1, T2, T3, T4, T5, T6)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(7, 0, 1, 2, 3, 4, 5, 6);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7> FromNapiValue for (T0, T1, T2, T3, T4, T5, T6, T7)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(8, 0, 1, 2, 3, 4, 5, 6, 7);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> FromNapiValue for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(9, 0, 1, 2, 3, 4, 5, 6, 7, 8);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromNapiValue
|
||||
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
T9: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromNapiValue
|
||||
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
T9: FromNapiValue,
|
||||
T10: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromNapiValue
|
||||
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
T9: FromNapiValue,
|
||||
T10: FromNapiValue,
|
||||
T11: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> FromNapiValue
|
||||
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
T9: FromNapiValue,
|
||||
T10: FromNapiValue,
|
||||
T11: FromNapiValue,
|
||||
T12: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(13, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> FromNapiValue
|
||||
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
T9: FromNapiValue,
|
||||
T10: FromNapiValue,
|
||||
T11: FromNapiValue,
|
||||
T12: FromNapiValue,
|
||||
T13: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> FromNapiValue
|
||||
for (
|
||||
T0,
|
||||
T1,
|
||||
T2,
|
||||
T3,
|
||||
T4,
|
||||
T5,
|
||||
T6,
|
||||
T7,
|
||||
T8,
|
||||
T9,
|
||||
T10,
|
||||
T11,
|
||||
T12,
|
||||
T13,
|
||||
T14,
|
||||
)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
T9: FromNapiValue,
|
||||
T10: FromNapiValue,
|
||||
T11: FromNapiValue,
|
||||
T12: FromNapiValue,
|
||||
T13: FromNapiValue,
|
||||
T14: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
|
||||
}
|
||||
|
||||
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> FromNapiValue
|
||||
for (
|
||||
T0,
|
||||
T1,
|
||||
T2,
|
||||
T3,
|
||||
T4,
|
||||
T5,
|
||||
T6,
|
||||
T7,
|
||||
T8,
|
||||
T9,
|
||||
T10,
|
||||
T11,
|
||||
T12,
|
||||
T13,
|
||||
T14,
|
||||
T15,
|
||||
)
|
||||
where
|
||||
T0: FromNapiValue,
|
||||
T1: FromNapiValue,
|
||||
T2: FromNapiValue,
|
||||
T3: FromNapiValue,
|
||||
T4: FromNapiValue,
|
||||
T5: FromNapiValue,
|
||||
T6: FromNapiValue,
|
||||
T7: FromNapiValue,
|
||||
T8: FromNapiValue,
|
||||
T9: FromNapiValue,
|
||||
T10: FromNapiValue,
|
||||
T11: FromNapiValue,
|
||||
T12: FromNapiValue,
|
||||
T13: FromNapiValue,
|
||||
T14: FromNapiValue,
|
||||
T15: FromNapiValue,
|
||||
{
|
||||
tuple_from_napi_value!(16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,21 @@ pub enum Either<A, B> {
|
|||
B(B),
|
||||
}
|
||||
|
||||
unsafe impl<A: Send, B: Send> Send for Either<A, B> {}
|
||||
unsafe impl<A: Sync, B: Sync> Sync for Either<A, B> {}
|
||||
|
||||
impl<A: AsRef<T>, B: AsRef<T>, T> AsRef<T> for Either<A, B>
|
||||
where
|
||||
T: ?Sized,
|
||||
{
|
||||
fn as_ref(&self) -> &T {
|
||||
match &self {
|
||||
Self::A(a) => a.as_ref(),
|
||||
Self::B(b) => b.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: NapiRaw, B: NapiRaw> Either<A, B> {
|
||||
/// # Safety
|
||||
/// Backward compatible with `Either` in **v1**
|
||||
|
|
|
@ -3,9 +3,8 @@ use std::{
|
|||
ops::{Deref, DerefMut},
|
||||
};
|
||||
|
||||
use crate::{check_status, sys, Error, Status, TaggedObject};
|
||||
|
||||
use super::{FromNapiValue, ToNapiValue, TypeName, ValidateNapiValue};
|
||||
use crate::{check_status, sys, Error, Status, TaggedObject};
|
||||
|
||||
pub struct External<T: 'static> {
|
||||
obj: *mut TaggedObject<T>,
|
||||
|
@ -23,6 +22,12 @@ impl<T: 'static> TypeName for External<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> From<T> for External<T> {
|
||||
fn from(t: T) -> Self {
|
||||
External::new(t)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> ValidateNapiValue for External<T> {}
|
||||
|
||||
impl<T: 'static> External<T> {
|
||||
|
|
|
@ -126,7 +126,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> {
|
|||
))
|
||||
} else {
|
||||
let key = properties.get_element::<JsString>(0)?;
|
||||
let value: JsUnknown = js_object.get_property(&key)?;
|
||||
let value: JsUnknown = js_object.get_property(key)?;
|
||||
visitor.visit_enum(JsEnumAccess::new(
|
||||
key.into_utf8()?.into_owned()?,
|
||||
Some(&value.0),
|
||||
|
@ -354,7 +354,7 @@ impl<'de, 'env> MapAccess<'de> for JsObjectAccess<'env> {
|
|||
));
|
||||
}
|
||||
let prop_name = self.properties.get_element::<JsString>(self.idx)?;
|
||||
let value: JsUnknown = self.value.get_property(&prop_name)?;
|
||||
let value: JsUnknown = self.value.get_property(prop_name)?;
|
||||
|
||||
self.idx += 1;
|
||||
let mut de = De(&value.0);
|
||||
|
|
Loading…
Reference in a new issue