Crate castflip_derive
Crate castflip_derive provides three derive macros to implement the basic traits of crate castflip for their supported types.
Crate castflip
Crate castflip is a Rust library for encoding and decoding numeric variables, arrays and structures. It provides methods to convert between a byte representation of a fixed binary format and a value of a Rust type with endianness handling.
More precisely, crate castflip provides several traits
- to encast a byte representation of a type as a value of the type with endianness handling,
- to decast a value of a type as a byte representation of the type with endianness handling, and
- to be used as bounds to determine which methods can be applied.
The supported types include
- primitive numeric types, and
- array types,
structtypes anduniontypes consisting of the supported types.
For more information, please see the documentation of crate castflip. It includes a number of examples and summaries as well as the detailed descriptions of its types and its traits.
A Simple Example
The example below encasts a byte representation of the UDP
header in big-endian as a value of struct UdpHdr in native-endian.
use ;
//
// Step 1: Define struct `UdpHdr` (The UDP header) and test data.
//
// to make it possible to apply #[derive(Cast)]
// to implement trait Cast and trait Flip
// Input: A sample byte representation of the UDP header (8 bytes)
let in_bytes: = ;
//
// Step 2: Encast a byte representation of the UDP header in big-endian
// (`BE`) at the head of variable `in_bytes` as a value of struct
// `UdpHdr` in native-endian and save it to variable `out_hdr`.
//
let out_hdr: UdpHdr = in_bytes.encastf.unwrap;
// Check if all fields in variable `out_hdr` are as expected.
assert_eq!; // = 50121 (Ephemeral Port)
assert_eq!; // = 53 (DNS Port)
assert_eq!; // = 50 (Length in Bytes)
assert_eq!; // = 0x823f (Checksum)
To use crate castflip version 0.1, add the following lines to your
Cargo.toml:
[]
= "0.1"