pub trait Serializable: Sized {
// Required methods
fn to_json(&self) -> SerializationResult<String>;
fn from_json(json: &str) -> SerializationResult<Self>;
fn to_binary(&self) -> SerializationResult<Vec<u8>>;
fn from_binary(data: &[u8]) -> SerializationResult<Self>;
// Provided methods
fn save<P: AsRef<Path>>(
&self,
path: P,
format: Format,
) -> SerializationResult<()> { ... }
fn save_to_writer<W: Write>(
&self,
writer: &mut W,
format: Format,
) -> SerializationResult<()> { ... }
fn load<P: AsRef<Path>>(
path: P,
format: Format,
) -> SerializationResult<Self> { ... }
fn load_from_reader<R: Read>(
reader: &mut R,
format: Format,
) -> SerializationResult<Self> { ... }
}Expand description
Core serialization trait for Train Station objects
This trait provides a unified interface for saving and loading objects in multiple formats. All serializable objects must implement this trait to enable persistent storage and model checkpointing. The trait includes both file-based and writer-based operations for maximum flexibility.
§Required Methods
to_json- Serialize the object to JSON formatfrom_json- Deserialize an object from JSON formatto_binary- Serialize the object to binary formatfrom_binary- Deserialize an object from binary format
§Provided Methods
save- Save the object to a file in the specified formatsave_to_writer- Save the object to a writer in the specified formatload- Load an object from a file in the specified formatload_from_reader- Load an object from a reader in the specified format
§Safety
Implementations must ensure that:
- Serialized data contains all necessary information for reconstruction
- Deserialization validates all input data thoroughly
- Memory safety is maintained during reconstruction
- No undefined behavior occurs with malformed input
§Examples
use train_station::serialization::{Serializable, Format, SerializationResult};
// Example implementation for a simple struct
#[derive(Debug, PartialEq)]
struct SimpleData {
value: i32,
}
impl Serializable for SimpleData {
fn to_json(&self) -> SerializationResult<String> {
Ok(format!(r#"{{"value":{}}}"#, self.value))
}
fn from_json(json: &str) -> SerializationResult<Self> {
// Simple parsing for demonstration
if let Some(start) = json.find("value\":") {
let value_str = &json[start + 7..];
if let Some(end) = value_str.find('}') {
let value: i32 = value_str[..end].parse()
.map_err(|_| "Invalid number format")?;
return Ok(SimpleData { value });
}
}
Err("Invalid JSON format".into())
}
fn to_binary(&self) -> SerializationResult<Vec<u8>> {
Ok(self.value.to_le_bytes().to_vec())
}
fn from_binary(data: &[u8]) -> SerializationResult<Self> {
if data.len() != 4 {
return Err("Invalid binary data length".into());
}
let value = i32::from_le_bytes([data[0], data[1], data[2], data[3]]);
Ok(SimpleData { value })
}
}
// Usage example
let data = SimpleData { value: 42 };
let json = data.to_json().unwrap();
let loaded = SimpleData::from_json(&json).unwrap();
assert_eq!(data, loaded);§Implementors
Common types that implement this trait include:
Tensor- For serializing tensor data and metadataAdamConfig- For serializing optimizer configurationSerializableAdam- For serializing optimizer state
Required Methods§
Sourcefn to_json(&self) -> SerializationResult<String>
fn to_json(&self) -> SerializationResult<String>
Serialize the object to JSON format
This method converts the object into a human-readable JSON string representation. The JSON format is suitable for debugging, configuration files, and cross-language interoperability.
§Returns
JSON string representation of the object on success, or SerializationError on failure
Sourcefn from_json(json: &str) -> SerializationResult<Self>
fn from_json(json: &str) -> SerializationResult<Self>
Deserialize an object from JSON format
This method parses a JSON string and reconstructs an object of the implementing type. The JSON must contain all necessary fields and data in the expected format.
§Arguments
json- JSON string containing serialized object
§Returns
The deserialized object on success, or SerializationError on failure
Sourcefn to_binary(&self) -> SerializationResult<Vec<u8>>
fn to_binary(&self) -> SerializationResult<Vec<u8>>
Serialize the object to binary format
This method converts the object into a compact binary representation optimized for storage and transmission. The binary format provides maximum performance and minimal file sizes.
§Returns
Binary representation of the object on success, or SerializationError on failure
Sourcefn from_binary(data: &[u8]) -> SerializationResult<Self>
fn from_binary(data: &[u8]) -> SerializationResult<Self>
Deserialize an object from binary format
This method parses binary data and reconstructs an object of the implementing type. The binary data must contain complete serialized information in the expected format.
§Arguments
data- Binary data containing serialized object
§Returns
The deserialized object on success, or SerializationError on failure
Provided Methods§
Sourcefn save<P: AsRef<Path>>(
&self,
path: P,
format: Format,
) -> SerializationResult<()>
fn save<P: AsRef<Path>>( &self, path: P, format: Format, ) -> SerializationResult<()>
Save the object to a file in the specified format
This method creates or overwrites a file at the specified path and writes the serialized object data in the requested format. The file is created with write permissions and truncated if it already exists.
§Arguments
path- File path where the object should be savedformat- Serialization format (JSON or Binary)
§Returns
Ok(()) on success, or SerializationError on failure
§Examples
use train_station::serialization::{Serializable, Format, SerializationResult};
use std::io::Write;
// Simple example struct
struct TestData { value: i32 }
impl Serializable for TestData {
fn to_json(&self) -> SerializationResult<String> {
Ok(format!(r#"{{"value":{}}}"#, self.value))
}
fn from_json(json: &str) -> SerializationResult<Self> {
Ok(TestData { value: 42 }) // Simplified for example
}
fn to_binary(&self) -> SerializationResult<Vec<u8>> {
Ok(self.value.to_le_bytes().to_vec())
}
fn from_binary(_data: &[u8]) -> SerializationResult<Self> {
Ok(TestData { value: 42 }) // Simplified for example
}
}
let data = TestData { value: 42 };
// Save to temporary file (cleanup handled by temp directory)
let temp_dir = std::env::temp_dir();
let json_path = temp_dir.join("test_data.json");
data.save(&json_path, Format::Json).unwrap();
// Verify file was created
assert!(json_path.exists());
// Clean up
std::fs::remove_file(&json_path).ok();Sourcefn save_to_writer<W: Write>(
&self,
writer: &mut W,
format: Format,
) -> SerializationResult<()>
fn save_to_writer<W: Write>( &self, writer: &mut W, format: Format, ) -> SerializationResult<()>
Save the object to a writer in the specified format
This method serializes the object and writes the data to the provided writer. The writer is flushed after writing to ensure all data is written. This method is useful for streaming serialization or writing to non-file destinations.
§Arguments
writer- Writer to output serialized dataformat- Serialization format (JSON or Binary)
§Returns
Ok(()) on success, or SerializationError on failure
Sourcefn load<P: AsRef<Path>>(path: P, format: Format) -> SerializationResult<Self>
fn load<P: AsRef<Path>>(path: P, format: Format) -> SerializationResult<Self>
Load an object from a file in the specified format
This method reads the entire file content and deserializes it into an object of the implementing type. The file must exist and contain valid serialized data in the specified format.
§Arguments
path- File path to read fromformat- Expected serialization format
§Returns
The deserialized object on success, or SerializationError on failure
§Examples
use train_station::serialization::{Serializable, Format, SerializationResult};
use std::io::Write;
// Simple example struct
#[derive(Debug, PartialEq)]
struct TestData { value: i32 }
impl Serializable for TestData {
fn to_json(&self) -> SerializationResult<String> {
Ok(format!(r#"{{"value":{}}}"#, self.value))
}
fn from_json(json: &str) -> SerializationResult<Self> {
// Simple parsing for demonstration
if json.contains("42") {
Ok(TestData { value: 42 })
} else {
Ok(TestData { value: 0 })
}
}
fn to_binary(&self) -> SerializationResult<Vec<u8>> {
Ok(self.value.to_le_bytes().to_vec())
}
fn from_binary(data: &[u8]) -> SerializationResult<Self> {
if data.len() >= 4 {
let value = i32::from_le_bytes([data[0], data[1], data[2], data[3]]);
Ok(TestData { value })
} else {
Ok(TestData { value: 0 })
}
}
}
let original = TestData { value: 42 };
// Save and load from temporary file
let temp_dir = std::env::temp_dir();
let json_path = temp_dir.join("test_load.json");
original.save(&json_path, Format::Json).unwrap();
let loaded = TestData::load(&json_path, Format::Json).unwrap();
assert_eq!(original, loaded);
// Clean up
std::fs::remove_file(&json_path).ok();Sourcefn load_from_reader<R: Read>(
reader: &mut R,
format: Format,
) -> SerializationResult<Self>
fn load_from_reader<R: Read>( reader: &mut R, format: Format, ) -> SerializationResult<Self>
Load an object from a reader in the specified format
This method reads all available data from the provided reader and deserializes it into an object of the implementing type. The reader must contain complete serialized data in the specified format.
§Arguments
reader- Reader containing serialized dataformat- Expected serialization format
§Returns
The deserialized object on success, or SerializationError on failure
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.