feat(napi): implement from_vec for Array

This commit is contained in:
LongYinan 2021-11-22 18:31:51 +08:00
parent c77712e76f
commit f83e167bd2
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
2 changed files with 18 additions and 2 deletions

View file

@ -26,8 +26,9 @@ impl NapiConst {
&format!("__register__const__{}_callback__", register_name),
self.name.span(),
);
quote! {
#[allow(non_snake_case)]
#[allow(clippy::all)]
unsafe fn #cb_name(env: napi::sys::napi_env) -> napi::Result<napi::sys::napi_value> {
<#type_name as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #name_ident)
}

View file

@ -1,6 +1,7 @@
use crate::{bindgen_prelude::*, check_status, sys, ValueType};
use std::ptr;
use crate::{bindgen_prelude::*, check_status, sys, ValueType};
pub struct Array {
env: sys::napi_env,
inner: sys::napi_value,
@ -116,6 +117,20 @@ impl FromNapiValue for Array {
}
}
impl Array {
pub fn from_vec<T>(env: &Env, value: Vec<T>) -> Result<Self>
where
T: ToNapiValue,
{
let mut arr = Array::new(env.0, value.len() as u32)?;
value.into_iter().try_for_each(|val| {
arr.insert(val)?;
Ok::<(), Error>(())
})?;
Ok(arr)
}
}
impl ValidateNapiValue for Array {
fn type_of() -> Vec<ValueType> {
vec![ValueType::Object]