feat(napi): implement from_vec
for Array
This commit is contained in:
parent
c77712e76f
commit
f83e167bd2
2 changed files with 18 additions and 2 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue