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

Unsized

User-defined types

Sized struct

```rust

[flatty::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).

```rust

[flatty::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.

```rust

[flatty::flat(sized = false)]

struct UnsizedStruct { a: u8, b: u16, c: flatty::FlatVec, }

```

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.

```rust

[flatty::flat(sized = false)]

enum UnsizedEnum { A, B(u8, u16), C { a: u8, b: flatty::FlatVec }, } ```

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.