1 unstable release

Uses new Rust 2021

0.1.0-alpha.2 Sep 9, 2022

#536 in Rust patterns

Download history 30/week @ 2022-09-05 19/week @ 2022-09-12 9/week @ 2022-09-19 6/week @ 2022-09-26

64 downloads per month
Used in flatty

MIT/Apache

48KB
1K SLoC

flatty

Crates.io Docs.rs Github Actions License

Flat message buffers.

Overview

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

Sized

  • 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).

Unsized

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

User-defined types

Sized struct

#[flatty::make_flat]
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::make_flat(enum_type = "u32")]
enum SizedEnum {
    A,
    B(u16, u8),
    C { a: u8, b: u16 },
    D(u32),
}

Unsized struct

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

#[flatty::make_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::make_flat(sized = false)]
enum UnsizedEnum {
    A,
    B(u8, u16),
    C { a: u8, b: flatty::FlatVec<u8, u16> },
}

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~185KB