5 releases

0.1.0-alpha.4 Feb 18, 2023
0.1.0-alpha.3 Oct 21, 2022
0.1.0-alpha.2 Sep 9, 2022
0.1.0-alpha.1 Aug 30, 2022
0.1.0-alpha.0 Aug 25, 2022

#394 in Rust patterns

42 downloads per month
Used in flatty-io


1.5K SLoC


Crates.io Docs.rs Github Actions License

Flat message buffers.


The crate provides basic flat types and a macro to create new flat types. Flat means that it occupies a single contiguous memory area.

Flat types have stable binary representation can be safely transferred between machines (of the same endianness) as is without packing/unpacking.

Message is represented as native Rust struct or enum.

Basic types


  • Unit type (()).
  • Signed and unsigned integers (u8, i8, u16, i16, u32, i32, u64, i64, u128, i128).
  • Floating-point numbers (f32, f64).
  • Array of some sized flat type ([T; N] where T: FlatSized).


  • Flat vector (FlatVec<T, L = u32>).

User-defined types

Sized struct

struct SizedStruct {
    a: u8,
    b: u16,
    c: u32,
    d: [u64; 4],

Sized enum

For enum you may explicitly set the type of variant index (default value is u8).

#[flatty::flat(enum_type = "u32")]
enum SizedEnum {
    B(u16, u8),
    C { a: u8, b: u16 },

Unsized struct

Unsized struct is DST. The reference to that structure contains its size.

#[flatty::flat(sized = false)]
struct UnsizedStruct {
    a: u8,
    b: u16,
    c: flatty::FlatVec<u64>,

Unsized enum

Rust doesn't support DST enums yet so for now enum declaration is translated to unsized structure.

But it has as_ref/as_mut methods that returns a native enum that contains references to original enum fields.

#[flatty::flat(sized = false)]
enum UnsizedEnum {
    B(u8, u16),
    C { a: u8, b: flatty::FlatVec<u8, u16> },


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~29K SLoC