lukaslueg / restruct Goto Github PK
View Code? Open in Web Editor NEWSimple parsers for binary data in Rust
License: MIT License
Simple parsers for binary data in Rust
License: MIT License
We compute alignment via std::mem::align_of
. While this seems to work, the approach is completely naive and almost certainly wrong.
Identify cases where this breaks so we can at least document them. Provide tests that prove edge cases in either the positive or the negative.
Hello,
very cool project!
Does this project need the nightly features? I would be humbled if you could find a way to disable the use of nightly features. Otherwise it is not possible to use the project on the stable channel.
I can also help :)
Best greetings
Julian
Hello,
the example code fails with the error message:
$ cargo +nightly run
Compiling test_restruct v0.1.0 (XXX)
error[E0433]: failed to resolve: use of undeclared crate or module `restruct`
--> src/main.rs:8:14
|
8 | #[derive(restruct_derive::Struct)]
| ^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `restruct`
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: unused import: `restruct_derive::Struct`
--> src/main.rs:1:5
|
1 | use restruct_derive::Struct;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: aborting due to previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0433`.
error: could not compile `test_restruct`
To learn more, run the command again with --verbose.
use restruct_derive::Struct;
fn main() {
#![feature(const_fn)]
#![feature(const_transmute)]
// Generate a parser in little-endian for two 32bit integers, a float and a bool.
#[derive(restruct_derive::Struct)]
#[fmt="<2if?"]
struct FooParser;
// Pack a tuple of two integers, a float an a bool into a [u8; _]-buffer.
let packed = FooParser::pack((1, 2, 3.0, false));
assert_eq!(packed.len(), FooParser::SIZE);
// Packing and unpacking can't fail at runtime.
let unpacked = FooParser::unpack(packed);
assert_eq!(unpacked, (1, 2, 3.0, false));
}
I'm sorry if this just a user error :/
We currently go from a [u8; _]
directly to a normal (...)
-tuple and back. This works as long as their sizes match exactly (transmute will compile-error otherwise) but still leaves room open for error: A future compiler might decide to re-order the memory representation of a tuple-type (e.g. in order to avoid excessive alignment). The result will be safe behavior but completely garbage outcome.
The pack()
- and unpack()
-function should indirect via a repr(C) struct
when going from/to a [u8;_]
and convert that value again. This should always work and, in case the types actually match up exactly, compile to nothing.
Hi,
I'll publish a minimal example later tomorrow - its late here, but I'm trying to pack a single u8
variable, which probably is unusual - I'm just implementing the protocol I've been given.
Rust seems to think the ::pack(T)
function is expecting an empty value (or a tuple..?). But when I do ::pack((val))
, that makes no difference.
Apologies if this is a silly question though - any ideas?
Thanks.
Hey!
I noticed that the crate published at crates.io
is a bit outdated - it's using deprecated rustdoc macros which have been already removed in the repo. Would you mind publishing a new crate so that users of the lib can use the crate as a dependency (rather than github repo)? Thanks :)
The parser does not even try to identify valid rust identifiers like Foo123
or mod1::mod2::FooBar
. Expand and test the definition of ident
so it at least comes close to what Rust uses.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.