rust

napi-rs v2 to v3 migration for JsTypedArrayValue


Attempting to migrate napi-rs code from version v2 to v3.
napi::JsTypedArrayValue has be depreacted for napi::bindgen_prelude::Uint8Array/Int8Array...

How do I convert it individual arrays? Thx

pub fn from_typed_array(arr: &napi::JsTypedArrayValue) -> JsResult<Series> {
    let dtype: JsDataType = arr.typedarray_type.into();
    let series = match dtype {
        JsDataType::Int8 => typed_to_chunked!(arr, i8, Int8Type).into(),
        JsDataType::UInt8 => typed_to_chunked!(arr, u8, UInt8Type).into(),
        JsDataType::Int16 => typed_to_chunked!(arr, i16, Int16Type).into(),
        JsDataType::UInt16 => typed_to_chunked!(arr, u16, UInt16Type).into(),
        JsDataType::Int32 => typed_to_chunked!(arr, i32, Int32Type).into(),
        JsDataType::UInt32 => typed_to_chunked!(arr, u32, UInt32Type).into(),
        JsDataType::Float32 => typed_to_chunked!(arr, f32, Float32Type).into(),
        JsDataType::Float64 => typed_to_chunked!(arr, f64, Float64Type).into(),
        JsDataType::Int64 => typed_to_chunked!(arr, i64, Int64Type).into(),
        JsDataType::UInt64 => typed_to_chunked!(arr, u64, UInt64Type).into(),
        _ => panic!("cannot create series from"),
    };

    Ok(series)
}

Solution

  • You can use the TypedArray type instead:

    #[napi]
    pub fn accept_untyped_typed_array(input: TypedArray) -> usize {
      let TypedArray { typed_array_type, arraybuffer, byte_offset } = input;
      
      match typed_array_type {
        TypedArrayType::Int8 => {
          // deal with the arraybuffer
        }
        ...
      }
    }