ZeroCopySerialization

Trait ZeroCopySerialization 

Source
pub trait ZeroCopySerialization<A: ZeroCopySerializable> {
    // Required methods
    fn save_zero_copy(
        &self,
        path: impl AsRef<Path>,
        metadata: Option<Value>,
    ) -> CoreResult<()>;
    fn load_zero_copy(
        path: impl AsRef<Path>,
        mode: AccessMode,
    ) -> CoreResult<MemoryMappedArray<A>>;
    fn as_bytes_slice(&self) -> CoreResult<&[u8]>;
    fn as_bytes_slice_mut(&mut self) -> CoreResult<&mut [u8]>;
}
Expand description

An extension trait for MemoryMappedArray to support zero-copy serialization and deserialization.

This trait provides methods to save and load memory-mapped arrays with zero-copy operations, enabling efficient serialization of large datasets. The zero-copy approach allows data to be memory-mapped directly from files with minimal overhead.

Key features:

  • Efficient saving of arrays to disk with optional metadata
  • Near-instantaneous loading of arrays from disk via memory mapping
  • Support for different access modes (ReadOnly, ReadWrite, CopyOnWrite)
  • Direct access to raw byte representation for advanced use cases
  • Access to the array with specified dimensionality

Required Methods§

Source

fn save_zero_copy( &self, path: impl AsRef<Path>, metadata: Option<Value>, ) -> CoreResult<()>

Save the array to a file with zero-copy serialization.

This method serializes the memory-mapped array to a file, including:

  • A header with array information (type, shape, size)
  • Optional metadata as JSON (can be used for array description, creation date, etc.)
  • The raw binary data of the array
§Arguments
  • path - Path where the array will be saved
  • metadata - Optional metadata to include with the array (as JSON)
§Returns

CoreResult<()> indicating success or an error with context

§Example
let metadata = json!({"description": "Example array", "created": "2023-05-20"});
mmap.save_zero_copy("array.bin", Some(metadata))?;
Source

fn load_zero_copy( path: impl AsRef<Path>, mode: AccessMode, ) -> CoreResult<MemoryMappedArray<A>>

Load an array from a file with zero-copy deserialization.

This method memory-maps a file containing a previously serialized array, allowing near-instantaneous “loading” regardless of array size.

§Arguments
  • path - Path to the file containing the serialized array
  • mode - Access mode (ReadOnly, ReadWrite, or CopyOnWrite)
§Returns

A memory-mapped array or an error with context

§Example
let array = MemoryMappedArray::<f64>::load_zero_copy("array.bin", AccessMode::ReadOnly)?;
Source

fn as_bytes_slice(&self) -> CoreResult<&[u8]>

Get the raw byte representation of the array.

This provides low-level access to the memory-mapped data as a byte slice. Primarily used for implementing serialization operations.

§Returns

A byte slice representing the raw array data or an error

Source

fn as_bytes_slice_mut(&mut self) -> CoreResult<&mut [u8]>

Get the mutable raw byte representation of the array.

This provides low-level mutable access to the memory-mapped data. Primarily used for implementing serialization operations.

§Returns

A mutable byte slice or an error if the array is not mutable

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§