Debian Codegraph @

README.md

raw
1 rust-bitfield
2 =============
3
4 This crate provides macros to generate bitfield-like struct.
5
6 This a complete rewrite of the `bitfield` crate.
7 You can find the previous version in the [rust-bitfield-legacy](https://github.com/dzamlo/rust-bitfield-legacy) repository. This version works on the stable version of rustc and use a different syntax with different possibility.
8
9
10 ## Example
11
12 An IPv4 header could be described like that:
13
14 ```rust
15 bitfield!{
16 /// A IPV4 header
17 struct IpV4Header(MSB0 [u8]);
18 u32;
19 /// The version field
20 get_version, _: 3, 0;
21 /// The Internet Header Length (IHL) field
22 get_ihl, _: 7, 4;
23 /// ...
24 get_dscp, _: 13, 8;
25 get_ecn, _: 15, 14;
26 get_total_length, _: 31, 16;
27 get_identification, _: 47, 32;
28 get_df, _: 49;
29 get_mf, _: 50;
30 get_fragment_offset, _: 63, 51;
31 get_time_to_live, _: 71, 64;
32 get_protocol, _: 79, 72;
33 get_header_checksum, _: 95, 79;
34 get_source_address, _: 127, 96;
35 get_destination_address, _: 159, 128;
36 }
37 ```
38
39 In this example, all the fields are read-only, the _ as setter name signals to skip the setter method.
40 The range at the end (e.g. 3, 0) defines the bit range where the information is encoded.
41
42 ## Documentation
43
44 The documentation of the released version is available on [doc.rs](https://docs.rs/bitfield).
45
46 ## Things to watch out for
47
48 ### MSB and LSB ordering
49
50 When defining a field, the MSB (Most Sigificant Bit) comes first and the LSB (Least Sigificant Bit) second.
51
52 ### Sign extension and valid range of signed field
53
54 When the type of a field is signed (`i8`, `i16`, ...) sign extension is done when reading the field. THis means that if the MSB of the read bits is 1, the returned values will be negative.
55
56 This also means that the valid range of value for the field is different that a unsigend field. A 4 bits field got from -8 (-2<sup>3</sup>) to 7 (2<sup>3</sup>-1) instead of 0 to 15.
57
58 ## License
59
60 Licensed under either of
61
62 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
63 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
64
65 at your option.
66
67 ### Contribution
68
69 Unless you explicitly state otherwise, any contribution intentionally
70 submitted for inclusion in the work by you, as defined in the Apache-2.0
71 license, shall be dual licensed as above, without any additional terms or
72 conditions.