lunesrs/utils/
vectors.rs

1use wasm_bindgen::prelude::wasm_bindgen;
2
3/**
4# Convert a Vec u8 to Vec u32
5
6- Convert a vector of u8 in a new vector of u32
7
8## In JavaScript 👍
9
10```javascript
11import * as wasm from "lunesrs"
12
13const arrayu8: Uint8Array = new Uint8Array()
14const arrayu32: Uint32Array = wasm.toVecu32(arrayu8)
15```
16
17## In Rust 🤝
18
19```rust
20use lunesrs::utils::vectors::to_vecu32;
21use std::any::{Any, TypeId};
22
23let output = to_vecu32([1u8;100].to_vec());
24
25assert_eq!(
26    true,
27    output.iter().all(|x|
28        TypeId::of::<u32>() == x.type_id()
29    )
30);
31```
32*/
33#[wasm_bindgen(js_name = "toVecu32")]
34pub fn to_vecu32(arr: Vec<u8>) -> Vec<u32> {
35    arr.iter().map(|x| *x as u32).collect()
36}
37
38/**
39# Convert a Vec u32 to Vec u8
40
41- Convert a vector of u32 in a new vector of u8
42
43## In JavaScript 👍
44
45```javascript
46import * as wasm from "lunesrs"
47
48const arrayu32: Uint32Array = new Uint32Array()
49const arrayu8: Uint8Array = wasm.toVecu8(arrayu32)
50```
51
52## In Rust 🤝
53
54```rust
55use lunesrs::utils::vectors::to_vecu8;
56use std::any::{Any, TypeId};
57
58let output = to_vecu8([1u32;100].to_vec());
59
60assert_eq!(
61    true,
62    output.iter().all(|x|
63        TypeId::of::<u8>() == x.type_id()
64    )
65);
66```
67*/
68#[wasm_bindgen(js_name = "toVecu8")]
69pub fn to_vecu8(arr: Vec<u32>) -> Vec<u8> {
70    arr.iter().map(|x| *x as u8).collect()
71}