This crate defines the Serializable, Deserializable, and Versioned traits, and the
top-level serialization functions serialize and deserialize which require
Serializable and Deserializable to be implemented on their respective
arguments.
Serializable objects are either versioned or not. This is defined by a
const Option<Version> in the trait implementation for Versioned.
All calls to serialize and deserialize objects should be done with
serialize::serialize() and serialize::deserialize() respectively, as they
include checks against unused bytes in the reader object. Similarly all
deserialization implementations should not error when there are bytes remaining
as objects are often recursively deserialized.
In practice there are four classes of objects that will implement these traits:
- Top level versioned objects. These will include version information and
deserialization logic that branches on version information. If the object
has child elements implementing
Serializable/Deserializablethe type should overloadSerializable::serializeto ensure the proper serialization methods are called on any child elements - Unversioned objects (
String,u64, etc.). These can use theunversioned_serializeable!(type)macro to build out the boilerplate implementation for simple types. For complex types the boilerplate may need to be written by hand. Care should be taken to ensure any child elements implementingSerializable/Deserializablecall the correct serialization functions, this will require overloadingSerializable::serialize. - Standard containers containing
Serializable/Deserializabletypes. These have been implemented forVec,HashMapandOption. - Complex unversioned objects entirely containing
Serializable/Deserializableobjects. The child elements can be versioned or unversioned. TheSerializableandDeserializabletraits can be derived via a macro.
Serializable::serialize should not be overloaded, and instead one should
Serializable::serialize_inner with custom serialization logic if required
Running tests
NETWORK_ID=1 cargo test --release