dzamlo / rust-bitfield

This crate provides macros to generate bitfield-like struct.
Apache License 2.0
157 stars 19 forks source link

Trying to use it for my library but cannot compile. #31

Closed gotoco closed 2 years ago

gotoco commented 2 years ago

Hi! I am writing some helper library where I need to use bit fields to process some structures.

I started with the example code and simple test to see if I can write some tests, but unfortunately something is not working fine. I tried first example/bits_position.rs and it compiles/runs. Now when I am trying to rewrite example to be a test it fails to recognize BitsLocations and BitsLocationsMsb0.

My src/lib.rs is

#[macro_use]
extern crate bitfield;

use bitfield::BitMut;
use bitfield::BitRangeMut;

bitfield! {
        struct BitsLocations([u8]);
}

bitfield! {
        struct BitsLocationsMsb0(MSB0 [u8]);
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        let mut bits_locations = BitsLocations([0; 3]);
        let mut bits_locations_msb0 = BitsLocationsMsb0([0; 3]);

        assert_eq!(2 + 2, 4);
    }
}

This code is causing compilation error:

error[E0425]: cannot find function, tuple struct or tuple variant `BitsLocations` in this scope
  --> src/lib.rs:20:34
   |
20 |         let mut bits_locations = BitsLocations([0; 3]);
   |                                  ^^^^^^^^^^^^^ not found in this scope
   |
help: consider importing this tuple struct
   |
18 |     use crate::BitsLocations;
   |

error[E0425]: cannot find function, tuple struct or tuple variant `BitsLocationsMsb0` in this scope
  --> src/lib.rs:21:39
   |
21 |         let mut bits_locations_msb0 = BitsLocationsMsb0([0; 3]);
   |                                       ^^^^^^^^^^^^^^^^^ not found in this scope
   |
help: consider importing this tuple struct
   |
18 |     use crate::BitsLocationsMsb0;
   |

Appreciate any help or tips what I am doing wrong...

dzamlo commented 2 years ago

The generated struct is not visible in the the tests modules. Like in any submodules. See https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=96378b02cb8edf608e958dca0bd696d8 for a maybe simpler example. Like the compile error message says, you need to import it with the use statetement in the tests modules.

gotoco commented 2 years ago

Thanks, I wasn't aware of that. After using the use statement with correct structure solved the issue.

Closing the issue