Added big endian feature for little endian CPU#697
Added big endian feature for little endian CPU#697xtmono wants to merge 1 commit intoimmunant:masterfrom
Conversation
kkysen
left a comment
There was a problem hiding this comment.
Can you add a test showing how (and testing that) this works and would be used? You can put them alongside the other bitfields tests in tests/. Also, can you add documentation somewhere detailing how this can be used? Thanks!
| name: name.unwrap(), | ||
| ty: ty.unwrap(), | ||
| bits: (bits.unwrap(), bits_span.unwrap()), | ||
| endian: endian, |
There was a problem hiding this comment.
| endian: endian, | |
| endian, |
| let byte_index = if big_endian { | ||
| field.len() - 1 - (bit_index / 8) | ||
| } else { | ||
| bit_index / 8 | ||
| }; |
There was a problem hiding this comment.
| let byte_index = if big_endian { | |
| field.len() - 1 - (bit_index / 8) | |
| } else { | |
| bit_index / 8 | |
| }; | |
| let byte_index = bit_index / 8; | |
| let byte_index = if big_endian { | |
| field.len() - 1 - byte_index | |
| } else { | |
| byte_index | |
| }; |
There was a problem hiding this comment.
Also, can you refactor all 3 (the 2 below changes, too) of these same computations into some helper function?
| field | ||
| .endian | ||
| .as_ref() | ||
| .map_or(false, |endian| endian == "big") |
There was a problem hiding this comment.
Should it only check for big or should it have to be big, little, or nothing?
There was a problem hiding this comment.
endian could be an enum, and if it had a default, this could be .unwrap_or_default(). (I lack the context here to understand whether a default endianness makes sense at all; gut says no).
| let field_endians: Vec<_> = bitfields | ||
| .iter() | ||
| .map(|field| { | ||
| field | ||
| .endian | ||
| .as_ref() | ||
| .map_or(false, |endian| endian == "big") | ||
| }) | ||
| .collect(); |
There was a problem hiding this comment.
| let field_endians: Vec<_> = bitfields | |
| .iter() | |
| .map(|field| { | |
| field | |
| .endian | |
| .as_ref() | |
| .map_or(false, |endian| endian == "big") | |
| }) | |
| .collect(); | |
| let field_endians = bitfields | |
| .iter() | |
| .map(|field| { | |
| field | |
| .endian | |
| .as_ref() | |
| .map_or(false, |endian| endian == "big") | |
| }) | |
| .collect::<Vec<_>>(); |
Handle big endian data on little endian CPU.
Useful for dealing with network data.