sdd/kiddo

WebAssembly compatibility

Closed this issue · 1 comments

I'm trying to use this crate with wasm-bindgen, and it seems that using u32 indices does not work. Specifically this line

(u32::MAX - u32::MAX.overflowing_shr(1).0) as usize * bucket_size

panics if the crate is built on wasm target. Here's a minimal example to reproduce the issue:

use kiddo::{float::kdtree::KdTree};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct KdTreeWasm {
    tree: KdTree<f64, u64, 2, 32, u32>,
}

#[wasm_bindgen]
impl KdTreeWasm {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        let tree: KdTree<f64, u64, 2, 32, u32> = KdTree::with_capacity(5);
        Self { tree }
    }
}

and on the JavaScript side, simply just initialize:

import('../pkg/index.js').then({ KdTreeWasm } => {
    let tree = new KdTreeWasm();
});

and this'll panic at attempt to multiply with an overflow.

Is this behaviour expected? Am I missing something?

If I'm not mistaken, capacity_with_bucket_size only works with u32 if #[cfg(target_pointer_width = "64")].

sdd commented

Fix released in v4.1.1