Skip to main content

Module serialization

Module serialization 

Source
Expand description

Cross-language serialization protocol for SciRS2.

Provides a versioned binary format (.scirs2 files) that can be read by Python bindings, WASM modules, and native Rust code.

§Format

Header: 64 bytes, little-endian

  • Magic: b"SCIRS2\0\0" (8 bytes)
  • Version: u16 major, u16 minor (4 bytes)
  • Payload type: u8 (1 byte): 0=array, 1=model, 2=stats, 3=custom
  • Compression: u8 (1 byte): 0=none, 1=lz4, 2=zstd
  • Checksum: u32 CRC32 of uncompressed payload (4 bytes)
  • Payload length: u64 (8 bytes) — bytes actually stored on disk
  • Reserved: 38 bytes of zeros

Payload (variable length, may be compressed):

  • For arrays: dtype(u8) + ndim(u8) + shape(u64 each, little-endian) + raw element bytes (little-endian)
  • For models: JSON config + raw parameter bytes
  • For stats: key-value pairs with typed values
  • For custom: raw bytes (caller-defined format)

§Example

use scirs2_core::serialization::{save_array, load_array, CompressionType};
use ndarray::Array2;
use std::path::Path;

let data = Array2::<f32>::ones((3, 4)).into_dyn();
let path = Path::new("/tmp/test.scirs2");
save_array(path, &data, CompressionType::None).expect("should succeed");
let loaded = load_array::<f32>(path).expect("should succeed");
assert_eq!(data, loaded);

Structs§

Scirs2Header
Parsed .scirs2 file header (64 bytes).
Scirs2Reader
Low-level reader for .scirs2 files.
Scirs2Writer
Low-level writer for .scirs2 files.

Enums§

CompressionType
Compression algorithm applied to the payload bytes.
PayloadType
Identifies the kind of data stored in the .scirs2 payload.
SerializationError
Errors that can occur during .scirs2 serialization or deserialization.

Traits§

ArrayElement
Element type supported by the .scirs2 array payload.

Functions§

load_array
Load an n-dimensional array from a .scirs2 file.
save_array
Save an n-dimensional array to a .scirs2 file.