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
crates/napi/src/bindgen_runtime/js_values

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]