feat: added support to wasm (#94)
* test: run tests also in wasm targets * fix: install rand with wasm-bindgen por wasm targets * fix: use actual usize size to access buffer. * fix: do not run functions that create files in wasm. * test: do not run in wasm test that panics. Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
This commit is contained in:
+8
-2
@@ -8,9 +8,12 @@ pub mod digits;
|
||||
pub mod generator;
|
||||
pub mod iris;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use crate::math::num::RealNumber;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::io::prelude::*;
|
||||
|
||||
/// Dataset
|
||||
@@ -49,6 +52,8 @@ impl<X, Y> Dataset<X, Y> {
|
||||
}
|
||||
}
|
||||
|
||||
// Running this in wasm throws: operation not supported on this platform.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn serialize_data<X: RealNumber, Y: RealNumber>(
|
||||
dataset: &Dataset<X, Y>,
|
||||
@@ -85,9 +90,9 @@ pub(crate) fn deserialize_data(
|
||||
const USIZE_SIZE: usize = std::mem::size_of::<usize>();
|
||||
let (num_samples, num_features) = {
|
||||
let mut buffer = [0u8; USIZE_SIZE];
|
||||
buffer.copy_from_slice(&bytes[0..8]);
|
||||
buffer.copy_from_slice(&bytes[0..USIZE_SIZE]);
|
||||
let num_features = usize::from_le_bytes(buffer);
|
||||
buffer.copy_from_slice(&bytes[8..16]);
|
||||
buffer.copy_from_slice(&bytes[8..8 + USIZE_SIZE]);
|
||||
let num_samples = usize::from_le_bytes(buffer);
|
||||
(num_samples, num_features)
|
||||
};
|
||||
@@ -116,6 +121,7 @@ pub(crate) fn deserialize_data(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn as_matrix() {
|
||||
let dataset = Dataset {
|
||||
|
||||
Reference in New Issue
Block a user