Skip to content

Commit 1ea2bbf

Browse files
committed
Begin the bitfield test suite
1 parent 34f3b1a commit 1ea2bbf

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

bitfield/tests/01-parse.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Our design for #[bitfield] (see the readme) involves marker types B1 through
2+
// B64 to indicate the bit width of each field.
3+
//
4+
// It would be possible to implement this without having any actual types B1
5+
// through B64 -- the attribute macro could recognize the names "B1" through
6+
// "B64" and deduce the bit width from the number in the name. But this hurts
7+
// composability! Later we'll want to make bitfield members out of other things,
8+
// like enums or type aliases which won't necessarily have a width in their
9+
// name:
10+
//
11+
// #[bitfield]
12+
// struct RedirectionTableEntry {
13+
// vector: B8,
14+
// dest_mode: DestinationMode,
15+
// trigger_mode: TriggerMode,
16+
// destination: Destination,
17+
// }
18+
//
19+
// #[bitfield]
20+
// enum DestinationMode {
21+
// Physical = 0,
22+
// Logical = 1,
23+
// }
24+
//
25+
// #[bitfield]
26+
// enum TriggerMode {
27+
// Edge = 0,
28+
// Level = 1,
29+
// }
30+
//
31+
// #[target_pointer_width = "64"]
32+
// type Destination = B30;
33+
//
34+
// #[target_pointer_width = "32"]
35+
// type Destination = B22;
36+
//
37+
// So instead of parsing a bit width from the type name, the approach we will
38+
// follow will hold bit widths in an associated constant of a trait that is
39+
// implemented for legal bitfield specifier types, including B1 through B64.
40+
//
41+
// Create a trait called bitfield::Specifier with an associated constant BITS,
42+
// and write a function-like procedural macro to define some types B1 through
43+
// B64 with corresponding impls of the Specifier trait.
44+
//
45+
// Be aware that crates that have the "proc-macro" crate type are not allowed to
46+
// export anything other than procedural macros. The project skeleton for this
47+
// project has been set up with two crates, one for procedural macros and the
48+
// other an ordinary library crate for the Specifier trait and B types which
49+
// also re-exports from the procedural macro crate so that users can get
50+
// everything through one library.
51+
52+
use bitfield::*;
53+
54+
//#[bitfield]
55+
pub struct MyFourBytes {
56+
a: B1,
57+
b: B3,
58+
c: B4,
59+
d: B24,
60+
}
61+
62+
fn main() {
63+
assert_eq!(<B24 as Specifier>::BITS, 24);
64+
}

bitfield/tests/02-storage.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Write an attribute macro that replaces the struct in its input with a byte
2+
// array representation of the correct size. For example the invocation in the
3+
// test case below might expand to the following where the `size` expression is
4+
// computed by summing the Specifier::BITS constant of each field type.
5+
//
6+
// #[repr(C)]
7+
// pub struct MyFourBytes {
8+
// data: [u8; #size],
9+
// }
10+
//
11+
// Don't worry for now what happens if the total bit size is not a multiple of
12+
// 8 bits. We will come back to that later to make it a compile-time error.
13+
14+
use bitfield::*;
15+
16+
#[bitfield]
17+
pub struct MyFourBytes {
18+
a: B1,
19+
b: B3,
20+
c: B4,
21+
d: B24,
22+
}
23+
24+
fn main() {
25+
assert_eq!(std::mem::size_of::<MyFourBytes>(), 4);
26+
}

bitfield/tests/progress.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[test]
22
fn tests() {
33
let t = workshop::TestCases::new();
4-
//t.pass("tests/01-parse.rs");
4+
//t.pass("tests/01-specifier-types.rs");
5+
//t.pass("tests/02-storage.rs");
56
}

0 commit comments

Comments
 (0)