Simple packet-based protocol definitions in Rust.
- The
Parceltrait defines any type that can be serialized to a connection. - The
wiremodule deals with transmission ofParcels.
Examples
Get the raw bytes representing a parcel.
extern crate djin_protocol;
extern crate djin_protocol_derive;
;
use Parcel;
assert_eq!;
Enums
Every enum has a value for each variant used to distinguish between each variant. This can be as simple as a 32-bit integer representing the variant's index. This is called the discriminator.
String discriminators
extern crate djin_protocol;
extern crate djin_protocol_derive;
// This enum, like all enums, defaults to using String discriminators.
// This enum explicitly specifies a string discriminant.
//
// This is the default anyway and thus it is identical in layout
// to the previous enum.
By default, enums have String discriminators. This means that
when serializing enum values to and from bytes, unless otherwise
specified the variant's full name will be transmitted as a string.
This allows variants to be added, modified, or reordered in a backwards-compatible way.
Integer discriminators
extern crate djin_protocol;
extern crate djin_protocol_derive;
// An enum with integer discriminators.
use ;
// By default, integer discriminators are 32-bits.
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
It is possible to set the underlying integer type via the #[repr(<type>)] attribute.
extern crate djin_protocol;
extern crate djin_protocol_derive;
// Force discriminators to be 8-bit.
use ;
assert_eq!;
Discriminators can be overriden on a per-variant basis via
the #[protocol(discriminator(<value>))] attribute.
In the case of trivial enums with no fields,
the Variant = <discriminator> syntax may be used.
extern crate djin_protocol;
extern crate djin_protocol_derive;
// Force discriminators to be 8-bit.
// Force discriminators to be 8-bit.
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;