繁体   English   中英

Rust Polars WebAssembly CSVReader

[英]Rust Polars WebAssembly CSVReader

我在尝试上传 CSV 文件并使用 rust 在 web 装配极坐标中解析它时遇到以下问题。

谢谢

错误:

Grid.js:498 panicked at 'unsafe precondition(s) violated: ptr::read requires that the pointer argument is aligned and non-null', C:\Users\61414\.rustup\toolchains\nightly-2022-11-20-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\panicking.rs:89:58

我在下面有示例代码。

此外,我使用 Svelte 作为前端,但我认为这不会有太大的不同。

Rust:

    pub fn load_csv(&mut self, buff: &[u8]) -> String {

        let cursor = Cursor::new(buff);

        let lf = CsvReader::new(cursor).with_ignore_parser_errors(true).finish().unwrap().lazy();

        return lf.describe_plan();
    }

Typescript:

当文件上传到文件输入时,仅触发 function。

    function load_csv_file(event){

        const file = event.target.files[0]

        let reader = new FileReader();
        reader.readAsArrayBuffer(file);
        reader.onload = data1 => {
            console.log(data1.target.result)

            console.log(data.load_csv(new Uint8Array(data1.target.result)))
        };
    }

WASM 跟踪:

不确定如何获得更好的东西

Error
    at http://localhost:5173/rustFunctions/grid/pkg/Grid.js:504:21
    at logError (http://localhost:5173/rustFunctions/grid/pkg/Grid.js:134:18)
    at imports.wbg.__wbg_new_abda76e883ba8a5f (http://localhost:5173/rustFunctions/grid/pkg/Grid.js:503:66)
    at console_error_panic_hook::Error::new::h5d7996250e9efb8e (http://localhost:5173/rustFunctions/grid/pkg/grid_bg.wasm:wasm-function[154789]:0x30fc8e8)
    at console_error_panic_hook::hook_impl::h667dd0ae102fb048 (http://localhost:5173/rustFunctions/grid/pkg/grid_bg.wasm:wasm-function[29585]:0x1c5c717)
    at console_error_panic_hook::hook::hc3def586df00a6f2 (http://localhost:5173/rustFunctions/grid/pkg/grid_bg.wasm:wasm-function[169823]:0x31c84b3)
    at core::ops::function::Fn::call::h397369bf956b8712 (http://localhost:5173/rustFunctions/grid/pkg/grid_bg.wasm:wasm-function[149254]:0x30a5529)
    at <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hfcda8f4a283bd42a (http://localhost:5173/rustFunctions/grid/pkg/grid_bg.wasm:wasm-function[125833]:0x2ee5fac)
    at std::panicking::rust_panic_with_hook::h7e6939e50e26b51d (http://localhost:5173/rustFunctions/grid/pkg/grid_bg.wasm:wasm-function[914]:0x4641c8)
    at std::panicking::begin_panic_handler::{{closure}}::hfff77ccb8fcf1114 (http://localhost:5173/rustFunctions/grid/pkg/grid_bg.wasm:wasm-function[34760]:0x1e31219)

至今:

  • 我检查了数组缓冲区不是 null,检查 cursor using.is_empty() 并打印出 [u8],它与 CSV 解析器的 [u8] 数组匹配,当我在本地运行 polars rust 代码时没有 Wasm .
  • 正如我提到的,我在本地使用了相同的 rust 代码,但没有正确加载文件的 Wasm。

所以我猜想在使用 WASM 时创建 cursor 或传递给 polars 有问题,但这只是一个猜测。

您可以查看 ` js-polars实现以供参考。

通常在将 rust 函数暴露给 JS 时,您应该使用wasm-bindgen或通过extern "C"函数使用 ffi。 wasm-bindgen 比手动处理 memory 和指针更容易使用。

其他一些注意事项:

如果处理大型数据集,您会喜欢使用 wasm 和 rust 遇到一些最大 memory 限制。您可以通过更新.cargo/config.toml来修改它

启动一个 web 工作线程池来解析 CSV 也有很大的开销,您需要在一个工作线程中运行parse_csv function。 @polars/browser npm package 简化了很多,您可以在那里查看有关如何在浏览器中运行 polars 的一些指南。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM