Crate flatty

Source
Expand description

Flat message buffers with direct mapping to Rust types without packing/unpacking.

§Main traits

  • Flat - type that occupies a single contiguous memory area. Guaranteed to always have the same binary representation on the same platform.
  • Portable - flat type that has stable platform-independent binary representation and therefore it can be safely transfered between different platforms (of even different endianness).

§Basic types

§Sized

§Unsized

§User-defined types

User can create new composite types by using flat macro.

§Struct

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

§Enum

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

#[flatty::flat(tag_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::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 {
    A,
    B(u8, u16),
    C { a: u8, b: flatty::FlatVec<u8, u16> },
}

Re-exports§

pub use flatty_portable as portable;

Modules§

emplacer
Emplacer-related functionality.
error
Error type.
prelude
Now it’s just an alias to traits.
traits
All traits.
utils
Utility functions used by macros, so they must be publicly available.
vec
Flat vector itself and its helper types.

Macros§

flat_vec
Creates FlatVec emplacer from given array.

Structs§

Error
Flat type operation error.
FlatWrap
Wrapper for smart pointer to byte slice that maps it to flat type.

Traits§

Emplacer
In-place initializer of flat type.
Flat
Flat type.
FlatDefault
Flat types that can be initialized to default state.
FlatSized
Statically-sized flat type.
Portable
Type that can be safely transfered between different machines.
TrustedDeref
Extra guarantees for Deref and DerefMut.

Type Aliases§

FlatVec
Growable flat vector of sized items.

Attribute Macros§

flat
Attribute macro that creates a flat type from struct or enum declaration.