Skip to main content

SaveData

Trait SaveData 

Source
pub trait SaveData: Sized {
    // Required methods
    fn serialize(&self) -> Vec<u8> ;
    fn deserialize(data: &[u8]) -> Result<Self, SaveError>;
    fn save_size() -> usize;

    // Provided methods
    fn checksum(data: &[u8]) -> u16 { ... }
    fn validate(data: &[u8]) -> bool { ... }
}
Expand description

Game-specific save data that can be serialized, checksummed, and persisted.

Implementors define how their data is serialized to/from bytes and how large each save slot is. A default CRC16 checksum is provided.

Required Methods§

Source

fn serialize(&self) -> Vec<u8>

Serialize this game data to a byte vector.

The returned data must NOT include the checksum — that is appended automatically by SaveManager.

Source

fn deserialize(data: &[u8]) -> Result<Self, SaveError>

Deserialize game data from a byte slice.

The input does NOT include the checksum bytes — they are stripped by SaveManager before calling this method.

Source

fn save_size() -> usize

Total size in bytes of a single save slot, including the 2-byte checksum.

This must equal serialize().len() + 2.

Provided Methods§

Source

fn checksum(data: &[u8]) -> u16

Compute a CRC16 checksum over the given serialized game data.

Default implementation uses CRC-16/XMODEM (polynomial 0x1021, initial value 0x0000).

Source

fn validate(data: &[u8]) -> bool

Validate a complete save slot payload (game data + 2-byte checksum).

Default implementation splits off the trailing 2-byte checksum and compares it against checksum.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§