-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overriding the default endianness #14
Comments
Hi, env variableBy setting an environment variable, it should be possible to set a default endianness. inner attributesCustom inner attributes would be a solution, unfortunately they are not supported in Rust (tracking issue). Maybe there is a workaround: it should be possible to add proc_macro attributes that acts like a global switch, and change the default endianness (unless specified in the struct). Depending on how This would look like: // [...]
#[default_little_endian] // switch default to little-endian (starting from here)
#[derive(Nom)]
pub struct AA { // will be derived as LE
}
#[derive(Nom)]
pub struct BB { // will be derived as LE
}
#[derive(Nom)]
#[nom(BigEndian)] // override endianness for this struct
pub struct CC { // will be derived as BE
}
#[default_big_endian] // switch back to big-endian by default
#[derive(Nom)]
pub struct DD { // will be derived as BE
} Any thoughts? |
My understanding of how macros/custom attributes work exactly in Rust is still a bit limited, but the ideal solution would probably be something like (I am assuming that this is what you can possibly achieve with custom inner attributes):
The global switch solution does look good, given that you can do it on a file-per-file basis without the state leaking to other files. As most people would just use |
Setting an attribute to an inline mod would be nice, but currently there are some difficulties/limitations (due to the way
#[default_little_endian] // attribute accepted
pub mod parser {
pub struct S {
// ...
}
#[default_little_endian] // this will NOT work: non-inline modules in proc macro input are unstable
// see https://github.com/rust-lang/rust/issues/54727 for tracking issue
mod parser; I'm afraid this kind of subtle differences would be hard to explain and could cause many bugs
This is why I think the global switch is currently the least bad option. However, I need to determine what are the side effects, for example:
If the answer is "only the current file", then I think it's a working solution, and a good compromise. |
Update on this topic:
Currently, I'm tempted to add new custom derive attributes ( use nom_derive::NomLE as Nom;
// all structs using #[derive(Nom)] will use little-endian As this would probably mean important (and API) changes in the existing code, I'd like to think a bit before validating this. |
Just chiming in to say that providing |
Would it be possible to override the big endian default for the entire crate or a particular file/module, rather than having to specify
#[nom(LittleEndian)]
for every singlestruct
? It is currently pretty easy to miss specifying#[nom(LittleEndian)]
, and it is kind of difficult to detect this mistake without carefully looking at your code.The text was updated successfully, but these errors were encountered: