• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

flatty

Crates.io Docs.rs Github Actions License

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

Overview

Type called flat when it occupies a single contiguous memory area. Such types is useful when you need to store or send such object as a binary data while having convenient way to access its contents without serializing/deserializing.

This crate provides basic flat types and the way to create new user-defined composite flat types (using #[flat] attribute macro), which can be used almost like regular Rust structs or enums.

Also the crate can be used without std and even alloc.

Concepts

Conversion

Binary representation can be obtained from instances of flat type using as_bytes (and unsafe as_mut_bytes). Also bytes can be converted to flat types, see in-place initialization and validation.

DST

Flat type can be dynamically sized (like FlatVec), in that case it exploits Rust's ability to operate ?Sized types. User-defined flat struct can also be unsized (only last field is allowed to be unsized). Even flat enum can be unsized, but Rust doesn't natively support them yet so its contents could be accessed only via as_ref/as_mut methods returning a regular enum containing references to original enum contents.

In-place initialization

Sized types can be instantiated as usual Rust type. But in case of DST Rust cannot construct it in usual manner on stack because its size isn't known at compile time. Instead we may initialize such types onto given memory area.

To do this we can use so-called emplacer - something that can initialize object onto given memory. For sized types its instance is also emplacer.

Emplacer could be applied to raw bytes using new_in_place or replace existing struct contents using assign_in_place. Also some types has default emplacer and could be initialized in default state by default_in_place.

Validation

Not any combination of bytes are valid representation of flat type. For example, Bool has only two valid states: 0 and 1, or FlatVec length must not be greater than its capacity. validate can be used to check that data is valid for specific flat type, or from_bytes/from_mut_bytes also perform such check.

When you trust your data, then you can omit validation using unsafe from_bytes_unchecked/from_mut_bytes_unchecked, but this will cause an UB if data is invalid.

Portability

Flat type guarantee that it has the same binary representation on the platforms with same byte order, alignment and address width. If you need stronger guarantees you may use Portable types - they have the same binary representation on any platform and always aligned to byte. To make own flat type portable use #[flat(portable = true)]. Also this can be used to created packed flat types without alignment issues.

Examples

You can find some examples on how to create and use flat types in tests directory.

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.

More Repositories

1

ringbuf

Lock-free SPSC FIFO ring buffer with direct access to inner data
Rust
283
star
2

vecmat-rs

Low-dimensional vector and matrix structures and common operations for them
Rust
22
star
3

hypertrace

Real-time hyperbolic space ray tracer
Rust
14
star
4

hcomplex

Lightweight complex and hypercomplex algebra library
Rust
10
star
5

async-ringbuf

Code moved to ringbuf
Rust
8
star
6

flownn

Viscous liquid flow prediction using neural network
Jupyter Notebook
6
star
7

cltracer

Realtime OpenCL ray tracer
C
6
star
8

nevo

C++
5
star
9

particles

Particle simulation on GPU via OpenCL
C++
5
star
10

epics-ca

Rust bindings for EPICS Channel Access protocol
Rust
3
star
11

pyepics-asyncio

Async/await support for PyEpics
Python
3
star
12

microchat

c++ chat server which uses libmicrohttpd and mysql
C++
3
star
13

digit-recognizer

The digit recognizer based on neural network
C
2
star
14

async-atomic

Asynchronous notification of atomic value change
Rust
2
star
15

stavec

Generic stack-allocated vector with static capacity
Rust
2
star
16

request-channel

Async MPSC request-reponse channel
Rust
2
star
17

mandelnet

Mandelbrot fractal generation using convolutional neural network
Jupyter Notebook
1
star
18

lxi-rs

LXI protocol implementation in Rust
Rust
1
star
19

libgui

OpenGL based cross-platform GUI library
C++
1
star
20

libfont

Very simple library for font rasterization
C
1
star
21

tension

Tensor library with OpenCL acceleration
Rust
1
star
22

akadem

Vehicle physics simulation
C++
1
star
23

ocl-include

Simple preprocessor that implements #include mechanism for OpenCL source files
Rust
1
star
24

persistent-tcp

TCP stream that tries to reconnect when disconnected
Rust
1
star
25

tokio-lxi

LXI protocol abstractions for Tokio
Rust
1
star
26

physsol-rs

Rust
1
star
27

libclray

OpenCL based library for realtime raytracing
1
star
28

lock-free-static

Safe mutable reference to static variable that can be obtained only once
Rust
1
star
29

candle-approx

Approximate comparison of Candle tensors
Rust
1
star