首页 > 解决方案 > 为什么 js_sys::Atomics 操作需要 i32 类型的值?

问题描述

我正在尝试SharedArrayBuffer在生锈中使用。我想在 sab 的一部分中累积 n 个工人的输出。我发现我被限制为 int 数组类型,但我的数据存储为浮点数。我正在使用Atomics::add(&rc2, idx as u32, k)where k 是我的浮点数转换为 i32。我在所描述的每种方法中都收到了一些垃圾(当我试图从填充的 sab 中读取 float32 视图时)。我尝试过:1)to_ne_bytes + i32::from_be_bytes在我的浮动上使用,然后将其重新组装为 i32 2)使用std::mem::transmute::<f32, i32>(v);3)使用 to_bits / cast to u32 / from_bits / cast to i32 4)在 js sys github repo 中查找实现(此处为https:// /github.com/rustwasm/wasm-bindgen/blob/master/crates/js-sys/src/lib.rs),但似乎只有一些与外部代码的绑定,我不明白这段代码的位置,所以我可以检查为什么 Atomic add 需要 i32 以及它对它的作用。谢谢!

标签: rustwasm-bindgen

解决方案


推荐阅读