@@ -457,6 +457,46 @@ macro_rules! impl_typed_array {
457457 } ;
458458}
459459
460+ macro_rules! impl_from_slice {
461+ ( $name: ident, $rust_type: ident, $typed_array_type: expr) => {
462+ impl FromNapiValue for & mut [ $rust_type] {
463+ unsafe fn from_napi_value( env: sys:: napi_env, napi_val: sys:: napi_value) -> Result <Self > {
464+ let mut typed_array_type = 0 ;
465+ let mut length = 0 ;
466+ let mut data = ptr:: null_mut( ) ;
467+ let mut array_buffer = ptr:: null_mut( ) ;
468+ let mut byte_offset = 0 ;
469+ let mut ref_ = ptr:: null_mut( ) ;
470+ check_status!(
471+ unsafe { sys:: napi_create_reference( env, napi_val, 1 , & mut ref_) } ,
472+ "Failed to create reference from Buffer"
473+ ) ?;
474+ check_status!(
475+ unsafe {
476+ sys:: napi_get_typedarray_info(
477+ env,
478+ napi_val,
479+ & mut typed_array_type,
480+ & mut length,
481+ & mut data,
482+ & mut array_buffer,
483+ & mut byte_offset,
484+ )
485+ } ,
486+ "Get TypedArray info failed"
487+ ) ?;
488+ if typed_array_type != $typed_array_type as i32 {
489+ return Err ( Error :: new(
490+ Status :: InvalidArg ,
491+ format!( "Expected $name, got {}" , typed_array_type) ,
492+ ) ) ;
493+ }
494+ Ok ( unsafe { core:: slice:: from_raw_parts_mut( data as * mut $rust_type, length) } )
495+ }
496+ }
497+ } ;
498+ }
499+
460500unsafe extern "C" fn finalizer < Data , T : Finalizer < RustType = Data > > (
461501 _env : sys:: napi_env ,
462502 finalize_data : * mut c_void ,
@@ -494,18 +534,30 @@ enum DataManagedType {
494534}
495535
496536impl_typed_array ! ( Int8Array , i8 , TypedArrayType :: Int8 ) ;
537+ impl_from_slice ! ( Int8Array , i8 , TypedArrayType :: Int8 ) ;
497538impl_typed_array ! ( Uint8Array , u8 , TypedArrayType :: Uint8 ) ;
539+ impl_from_slice ! ( Uint8Array , u8 , TypedArrayType :: Uint8 ) ;
498540impl_typed_array ! ( Uint8ClampedArray , u8 , TypedArrayType :: Uint8Clamped ) ;
499541impl_typed_array ! ( Int16Array , i16 , TypedArrayType :: Int16 ) ;
542+ impl_from_slice ! ( Int16Array , i16 , TypedArrayType :: Int16 ) ;
500543impl_typed_array ! ( Uint16Array , u16 , TypedArrayType :: Uint16 ) ;
544+ impl_from_slice ! ( Uint16Array , u16 , TypedArrayType :: Uint16 ) ;
501545impl_typed_array ! ( Int32Array , i32 , TypedArrayType :: Int32 ) ;
546+ impl_from_slice ! ( Int32Array , i32 , TypedArrayType :: Int32 ) ;
502547impl_typed_array ! ( Uint32Array , u32 , TypedArrayType :: Uint32 ) ;
548+ impl_from_slice ! ( Uint32Array , u32 , TypedArrayType :: Uint32 ) ;
503549impl_typed_array ! ( Float32Array , f32 , TypedArrayType :: Float32 ) ;
550+ impl_from_slice ! ( Float32Array , f32 , TypedArrayType :: Float32 ) ;
504551impl_typed_array ! ( Float64Array , f64 , TypedArrayType :: Float64 ) ;
552+ impl_from_slice ! ( Float64Array , f64 , TypedArrayType :: Float64 ) ;
505553#[ cfg( feature = "napi6" ) ]
506554impl_typed_array ! ( BigInt64Array , i64 , TypedArrayType :: BigInt64 ) ;
507555#[ cfg( feature = "napi6" ) ]
556+ impl_from_slice ! ( BigInt64Array , i64 , TypedArrayType :: BigInt64 ) ;
557+ #[ cfg( feature = "napi6" ) ]
508558impl_typed_array ! ( BigUint64Array , u64 , TypedArrayType :: BigUint64 ) ;
559+ #[ cfg( feature = "napi6" ) ]
560+ impl_from_slice ! ( BigUint64Array , u64 , TypedArrayType :: BigUint64 ) ;
509561
510562impl < T : Into < Vec < u8 > > > From < T > for Uint8Array {
511563 fn from ( data : T ) -> Self {
0 commit comments