binext
binext is a library that aims to make easier working with binary buffers and structs like you would in C.
This library provides safe interfaces to write/read structs from Read/Write binary sources.
If used along with #[repr(C)], this crate allows to read/write binary structures between C/C++ and Rust.
Motivation
I found interesting the way C/C++ allows to write/read a struct from a binary file just by casting a pointer
of it to a char*. I wanted to know if rust had any std implementation for that, or if there was a crate that
allowed me to do that, but i couldn't find any. So i just had to implement my own one! And here we are now!
Examples
Pure rust
Reading / writing to files from rust is pretty easy, let's take a look at an example:
use ;
use ;
C/C++ and Rust
In order to being able to use data written in both languages, the rust structures must be marked
as #[repr(C)], also, type size must be taken into account.
For example, a char in rust has a
size of 4 bytes, in C/C++ it has a size of 1 byte, so that must be taken into account, because if structure
sizes and/or alignments aren't the same, the data won't be correct.
In this example, we'll use C++ to write a struct into a file, then we'll read it from Rust.
C++ code:
using namespace std;
;
int
Rust code:
use BinaryRead;
use ;