Struct napi::bindgen_prelude::Array
source · pub struct Array { /* private fields */ }
Implementations§
source§impl Array
impl Array
sourcepub fn get<T: FromNapiValue>(&self, index: u32) -> Result<Option<T>>
pub fn get<T: FromNapiValue>(&self, index: u32) -> Result<Option<T>>
Examples found in repository?
src/bindgen_runtime/js_values/object.rs (line 75)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub fn keys(obj: &Object) -> Result<Vec<String>> {
let mut names = ptr::null_mut();
unsafe {
check_status!(
sys::napi_get_property_names(obj.0.env, obj.0.value, &mut names),
"Failed to get property names of given object"
)?;
}
let names = unsafe { Array::from_napi_value(obj.0.env, names)? };
let mut ret = vec![];
for i in 0..names.len() {
ret.push(names.get::<String>(i)?.unwrap());
}
Ok(ret)
}
More examples
src/bindgen_runtime/js_values/array.rs (line 272)
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
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)? };
let mut vec = vec![];
for i in 0..arr.len() {
if let Some(val) = arr.get::<T>(i)? {
vec.push(val);
} else {
return Err(Error::new(
Status::InvalidArg,
"Found inconsistent data type in Array<T> when converting to Rust Vec<T>".to_owned(),
));
}
}
Ok(vec)
}
sourcepub fn set<T: ToNapiValue>(&mut self, index: u32, val: T) -> Result<()>
pub fn set<T: ToNapiValue>(&mut self, index: u32, val: T) -> Result<()>
Examples found in repository?
src/bindgen_runtime/js_values/array.rs (line 64)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
pub fn insert<T: ToNapiValue>(&mut self, val: T) -> Result<()> {
self.set(self.len(), val)?;
Ok(())
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> u32 {
self.len
}
pub fn coerce_to_object(self) -> Result<JsObject> {
let mut new_raw_value = ptr::null_mut();
check_status!(unsafe { sys::napi_coerce_to_object(self.env, self.inner, &mut new_raw_value) })?;
Ok(JsObject(Value {
env: self.env,
value: new_raw_value,
value_type: ValueType::Object,
}))
}
}
impl TypeName for Array {
fn type_name() -> &'static str {
"Array"
}
fn value_type() -> ValueType {
ValueType::Object
}
}
impl ToNapiValue for Array {
unsafe fn to_napi_value(_env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
Ok(val.inner)
}
}
impl FromNapiValue for Array {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
let mut is_arr = false;
check_status!(
unsafe { sys::napi_is_array(env, napi_val, &mut is_arr) },
"Failed to check given napi value is array"
)?;
if is_arr {
let mut len = 0;
check_status!(
unsafe { sys::napi_get_array_length(env, napi_val, &mut len) },
"Failed to get Array length",
)?;
Ok(Array {
inner: napi_val,
env,
len,
})
} else {
Err(Error::new(
Status::InvalidArg,
"Given napi value is not an array".to_owned(),
))
}
}
}
impl Array {
/// Create `Array` from `Vec<T>`
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().enumerate().try_for_each(|(index, val)| {
arr.set(index as u32, val)?;
Ok::<(), Error>(())
})?;
Ok(arr)
}
/// Create `Array` from `&Vec<String>`
pub fn from_ref_vec_string(env: &Env, value: &[String]) -> Result<Self> {
let mut arr = Array::new(env.0, value.len() as u32)?;
value.iter().enumerate().try_for_each(|(index, val)| {
arr.set(index as u32, val.as_str())?;
Ok::<(), Error>(())
})?;
Ok(arr)
}
/// Create `Array` from `&Vec<T: Copy + ToNapiValue>`
pub fn from_ref_vec<T>(env: &Env, value: &[T]) -> Result<Self>
where
T: ToNapiValue + Copy,
{
let mut arr = Array::new(env.0, value.len() as u32)?;
value.iter().enumerate().try_for_each(|(index, val)| {
arr.set(index as u32, *val)?;
Ok::<(), Error>(())
})?;
Ok(arr)
}
}
impl ValidateNapiValue for Array {}
impl<T> TypeName for Vec<T> {
fn type_name() -> &'static str {
"Array<T>"
}
fn value_type() -> ValueType {
ValueType::Object
}
}
impl<T, const N: usize> ToNapiValue for [T; N]
where
T: ToNapiValue + Copy,
{
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.into_iter().enumerate() {
arr.set(i as u32, v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
impl<T> ToNapiValue for &[T]
where
T: ToNapiValue + Copy,
{
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.iter().enumerate() {
arr.set(i as u32, *v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
impl<T> ToNapiValue for Vec<T>
where
T: ToNapiValue,
{
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.into_iter().enumerate() {
arr.set(i as u32, v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
macro_rules! impl_for_primitive_type {
($primitive_type:ident) => {
impl ToNapiValue for &Vec<$primitive_type> {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.iter().enumerate() {
arr.set(i as u32, *v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
};
}
impl_for_primitive_type!(u8);
impl_for_primitive_type!(i8);
impl_for_primitive_type!(u16);
impl_for_primitive_type!(i16);
impl_for_primitive_type!(u32);
impl_for_primitive_type!(i32);
impl_for_primitive_type!(i64);
impl_for_primitive_type!(f64);
impl_for_primitive_type!(bool);
impl ToNapiValue for &Vec<String> {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.iter().enumerate() {
arr.set(i as u32, v.as_str())?;
}
unsafe { Array::to_napi_value(env, arr) }
}
pub fn insert<T: ToNapiValue>(&mut self, val: T) -> Result<()>
sourcepub fn len(&self) -> u32
pub fn len(&self) -> u32
Examples found in repository?
src/bindgen_runtime/js_values/array.rs (line 29)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
pub fn get<T: FromNapiValue>(&self, index: u32) -> Result<Option<T>> {
if index >= self.len() {
return Ok(None);
}
let mut ret = ptr::null_mut();
unsafe {
check_status!(
sys::napi_get_element(self.env, self.inner, index, &mut ret),
"Failed to get element with index `{}`",
index,
)?;
Ok(Some(T::from_napi_value(self.env, ret)?))
}
}
pub fn set<T: ToNapiValue>(&mut self, index: u32, val: T) -> Result<()> {
unsafe {
let napi_val = T::to_napi_value(self.env, val)?;
check_status!(
sys::napi_set_element(self.env, self.inner, index, napi_val),
"Failed to set element with index `{}`",
index,
)?;
if index >= self.len() {
self.len = index + 1;
}
Ok(())
}
}
pub fn insert<T: ToNapiValue>(&mut self, val: T) -> Result<()> {
self.set(self.len(), val)?;
Ok(())
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> u32 {
self.len
}
pub fn coerce_to_object(self) -> Result<JsObject> {
let mut new_raw_value = ptr::null_mut();
check_status!(unsafe { sys::napi_coerce_to_object(self.env, self.inner, &mut new_raw_value) })?;
Ok(JsObject(Value {
env: self.env,
value: new_raw_value,
value_type: ValueType::Object,
}))
}
}
impl TypeName for Array {
fn type_name() -> &'static str {
"Array"
}
fn value_type() -> ValueType {
ValueType::Object
}
}
impl ToNapiValue for Array {
unsafe fn to_napi_value(_env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
Ok(val.inner)
}
}
impl FromNapiValue for Array {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
let mut is_arr = false;
check_status!(
unsafe { sys::napi_is_array(env, napi_val, &mut is_arr) },
"Failed to check given napi value is array"
)?;
if is_arr {
let mut len = 0;
check_status!(
unsafe { sys::napi_get_array_length(env, napi_val, &mut len) },
"Failed to get Array length",
)?;
Ok(Array {
inner: napi_val,
env,
len,
})
} else {
Err(Error::new(
Status::InvalidArg,
"Given napi value is not an array".to_owned(),
))
}
}
}
impl Array {
/// Create `Array` from `Vec<T>`
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().enumerate().try_for_each(|(index, val)| {
arr.set(index as u32, val)?;
Ok::<(), Error>(())
})?;
Ok(arr)
}
/// Create `Array` from `&Vec<String>`
pub fn from_ref_vec_string(env: &Env, value: &[String]) -> Result<Self> {
let mut arr = Array::new(env.0, value.len() as u32)?;
value.iter().enumerate().try_for_each(|(index, val)| {
arr.set(index as u32, val.as_str())?;
Ok::<(), Error>(())
})?;
Ok(arr)
}
/// Create `Array` from `&Vec<T: Copy + ToNapiValue>`
pub fn from_ref_vec<T>(env: &Env, value: &[T]) -> Result<Self>
where
T: ToNapiValue + Copy,
{
let mut arr = Array::new(env.0, value.len() as u32)?;
value.iter().enumerate().try_for_each(|(index, val)| {
arr.set(index as u32, *val)?;
Ok::<(), Error>(())
})?;
Ok(arr)
}
}
impl ValidateNapiValue for Array {}
impl<T> TypeName for Vec<T> {
fn type_name() -> &'static str {
"Array<T>"
}
fn value_type() -> ValueType {
ValueType::Object
}
}
impl<T, const N: usize> ToNapiValue for [T; N]
where
T: ToNapiValue + Copy,
{
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.into_iter().enumerate() {
arr.set(i as u32, v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
impl<T> ToNapiValue for &[T]
where
T: ToNapiValue + Copy,
{
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.iter().enumerate() {
arr.set(i as u32, *v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
impl<T> ToNapiValue for Vec<T>
where
T: ToNapiValue,
{
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.into_iter().enumerate() {
arr.set(i as u32, v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
macro_rules! impl_for_primitive_type {
($primitive_type:ident) => {
impl ToNapiValue for &Vec<$primitive_type> {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.iter().enumerate() {
arr.set(i as u32, *v)?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
};
}
impl_for_primitive_type!(u8);
impl_for_primitive_type!(i8);
impl_for_primitive_type!(u16);
impl_for_primitive_type!(i16);
impl_for_primitive_type!(u32);
impl_for_primitive_type!(i32);
impl_for_primitive_type!(i64);
impl_for_primitive_type!(f64);
impl_for_primitive_type!(bool);
impl ToNapiValue for &Vec<String> {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let mut arr = Array::new(env, val.len() as u32)?;
for (i, v) in val.iter().enumerate() {
arr.set(i as u32, v.as_str())?;
}
unsafe { Array::to_napi_value(env, arr) }
}
}
impl<T> FromNapiValue for Vec<T>
where
T: FromNapiValue,
{
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)? };
let mut vec = vec![];
for i in 0..arr.len() {
if let Some(val) = arr.get::<T>(i)? {
vec.push(val);
} else {
return Err(Error::new(
Status::InvalidArg,
"Found inconsistent data type in Array<T> when converting to Rust Vec<T>".to_owned(),
));
}
}
Ok(vec)
}
More examples
src/bindgen_runtime/js_values/object.rs (line 74)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
pub fn keys(obj: &Object) -> Result<Vec<String>> {
let mut names = ptr::null_mut();
unsafe {
check_status!(
sys::napi_get_property_names(obj.0.env, obj.0.value, &mut names),
"Failed to get property names of given object"
)?;
}
let names = unsafe { Array::from_napi_value(obj.0.env, names)? };
let mut ret = vec![];
for i in 0..names.len() {
ret.push(names.get::<String>(i)?.unwrap());
}
Ok(ret)
}
pub fn coerce_to_object(self) -> Result<JsObject>
source§impl Array
impl Array
sourcepub fn from_vec<T>(env: &Env, value: Vec<T>) -> Result<Self>where
T: ToNapiValue,
pub fn from_vec<T>(env: &Env, value: Vec<T>) -> Result<Self>where
T: ToNapiValue,
Create Array
from Vec<T>
sourcepub fn from_ref_vec_string(env: &Env, value: &[String]) -> Result<Self>
pub fn from_ref_vec_string(env: &Env, value: &[String]) -> Result<Self>
Create Array
from &Vec<String>
sourcepub fn from_ref_vec<T>(env: &Env, value: &[T]) -> Result<Self>where
T: ToNapiValue + Copy,
pub fn from_ref_vec<T>(env: &Env, value: &[T]) -> Result<Self>where
T: ToNapiValue + Copy,
Create Array
from &Vec<T: Copy + ToNapiValue>
Trait Implementations§
source§impl FromNapiValue for Array
impl FromNapiValue for Array
source§unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> Result<Self>
unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> Result<Self>
Safety Read more
fn from_unknown(value: JsUnknown) -> Result<Self>
source§impl ToNapiValue for Array
impl ToNapiValue for Array
source§unsafe fn to_napi_value(_env: napi_env, val: Self) -> Result<napi_value>
unsafe fn to_napi_value(_env: napi_env, val: Self) -> Result<napi_value>
Safety Read more
source§impl ValidateNapiValue for Array
impl ValidateNapiValue for Array
source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Safety Read more