#bit-fields #bit #macro #struct #generate #version #ipv4

bitfield

This crate provides macros to generate bitfield-like struct

26 unstable releases (8 breaking)

Uses old Rust 2015

0.17.0 Sep 8, 2024
0.16.1 Jul 25, 2024
0.14.0 Jul 11, 2022
0.13.2 May 27, 2019
0.0.1 Nov 20, 2014

#40 in Rust patterns

Download history 75805/week @ 2024-08-04 74573/week @ 2024-08-11 75314/week @ 2024-08-18 78289/week @ 2024-08-25 84577/week @ 2024-09-01 76132/week @ 2024-09-08 75026/week @ 2024-09-15 74754/week @ 2024-09-22 67028/week @ 2024-09-29 65837/week @ 2024-10-06 76194/week @ 2024-10-13 77683/week @ 2024-10-20 75155/week @ 2024-10-27 81459/week @ 2024-11-03 74370/week @ 2024-11-10 76626/week @ 2024-11-17

311,892 downloads per month
Used in 1,575 crates (108 directly)

MIT/Apache

48KB
682 lines

rust-bitfield

This crate provides macros to generate bitfield-like struct.

This a complete rewrite of the bitfield crate. You can find the previous version in the rust-bitfield-legacy repository. This version works on the stable version of rustc and use a different syntax with different possibility.

Example

An IPv4 header could be described like that:

bitfield!{
    struct IpV4Header(MSB0 [u8]);
    u32;
    get_version, _: 3, 0;
    get_ihl, _: 7, 4;
    get_dscp, _: 13, 8;
    get_ecn, _: 15, 14;
    get_total_length, _: 31, 16;
    get_identification, _: 47, 32;
    get_df, _: 49;
    get_mf, _: 50;
    get_fragment_offset, _: 63, 51;
    get_time_to_live, _: 71, 64;
    get_protocol, _: 79, 72;
    get_header_checksum, _: 95, 79;
    get_source_address, _: 127, 96;
    get_destination_address, _: 159, 128;
}

In this example, all the fields are read-only, the _ as setter name signals to skip the setter method. The range at the end (e.g. 3, 0) defines the bit range where the information is encoded.

Documentation

The documentation of the released version is available on doc.rs.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps