sdd / kiddo

Kiddo
Apache License 2.0
79 stars 13 forks source link

WebAssembly compatibility #130

Closed Ptrskay3 closed 5 months ago

Ptrskay3 commented 7 months ago

I'm trying to use this crate with wasm-bindgen, and it seems that using u32 indices does not work. Specifically this line https://github.com/sdd/kiddo/blob/4e2dbf471229266a88ce3ef8dcbea38ebc0b76b0/src/types.rs#L80 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 5 months ago

Fix released in v4.1.1