bindgen 0.73.0

Automatically generates Rust FFI bindings to C and C++ libraries.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Tests for `__BindgenBitfieldUnit`.
//!
//! Note that bit-fields are allocated right to left (least to most significant
//! bits).
//!
//! From the x86 PS ABI:
//!
//! ```c
//! struct {
//!     int j : 5;
//!     int k : 6;
//!     int m : 7;
//! };
//! ```
//!
//! ```ignore
//! +------------------------------------------------------------+
//! |                     |              |            |          |
//! |       padding       |       m      |     k      |    j     |
//! |31                 18|17          11|10         5|4        0|
//! +------------------------------------------------------------+
//! ```

use super::bitfield_unit::__BindgenBitfieldUnit;

#[test]
fn bitfield_unit_get_bit() {
    let unit = __BindgenBitfieldUnit::<[u8; 2]>::new([0b10011101, 0b00011101]);

    let mut bits = vec![];
    for i in 0..16 {
        bits.push(unit.get_bit(i));
    }

    println!();
    println!("bits = {bits:?}");
    assert_eq!(
        bits,
        &[
            // 0b10011101
            true, false, true, true, true, false, false, true,
            // 0b00011101
            true, false, true, true, true, false, false, false
        ]
    );
}

#[test]
fn bitfield_unit_set_bit() {
    let mut unit =
        __BindgenBitfieldUnit::<[u8; 2]>::new([0b00000000, 0b00000000]);

    for i in 0..16 {
        if i % 3 == 0 {
            unit.set_bit(i, true);
        }
    }

    for i in 0..16 {
        assert_eq!(unit.get_bit(i), i % 3 == 0);
    }

    let mut unit =
        __BindgenBitfieldUnit::<[u8; 2]>::new([0b11111111, 0b11111111]);

    for i in 0..16 {
        if i % 3 == 0 {
            unit.set_bit(i, false);
        }
    }

    for i in 0..16 {
        assert_eq!(unit.get_bit(i), i % 3 != 0);
    }
}

macro_rules! bitfield_unit_get {
    (
        $(
            With $storage:expr , then get($start:expr, $len:expr) is $expected:expr;
        )*
    ) => {
        #[test]
        fn bitfield_unit_get() {
            $({
                let expected = $expected;
                let unit = __BindgenBitfieldUnit::<_>::new($storage);
                let actual = unit.get($start, $len);

                println!();
                println!("expected = {expected:064b}");
                println!("actual   = {actual:064b}");

                assert_eq!(expected, actual);
            })*
        }
   }
}

bitfield_unit_get! {
    // Let's just exhaustively test getting the bits from a single byte, since
    // there are few enough combinations...

    With [0b11100010], then get(0, 1) is 0;
    With [0b11100010], then get(1, 1) is 1;
    With [0b11100010], then get(2, 1) is 0;
    With [0b11100010], then get(3, 1) is 0;
    With [0b11100010], then get(4, 1) is 0;
    With [0b11100010], then get(5, 1) is 1;
    With [0b11100010], then get(6, 1) is 1;
    With [0b11100010], then get(7, 1) is 1;

    With [0b11100010], then get(0, 2) is 0b10;
    With [0b11100010], then get(1, 2) is 0b01;
    With [0b11100010], then get(2, 2) is 0b00;
    With [0b11100010], then get(3, 2) is 0b00;
    With [0b11100010], then get(4, 2) is 0b10;
    With [0b11100010], then get(5, 2) is 0b11;
    With [0b11100010], then get(6, 2) is 0b11;

    With [0b11100010], then get(0, 3) is 0b010;
    With [0b11100010], then get(1, 3) is 0b001;
    With [0b11100010], then get(2, 3) is 0b000;
    With [0b11100010], then get(3, 3) is 0b100;
    With [0b11100010], then get(4, 3) is 0b110;
    With [0b11100010], then get(5, 3) is 0b111;

    With [0b11100010], then get(0, 4) is 0b0010;
    With [0b11100010], then get(1, 4) is 0b0001;
    With [0b11100010], then get(2, 4) is 0b1000;
    With [0b11100010], then get(3, 4) is 0b1100;
    With [0b11100010], then get(4, 4) is 0b1110;

    With [0b11100010], then get(0, 5) is 0b00010;
    With [0b11100010], then get(1, 5) is 0b10001;
    With [0b11100010], then get(2, 5) is 0b11000;
    With [0b11100010], then get(3, 5) is 0b11100;

    With [0b11100010], then get(0, 6) is 0b100010;
    With [0b11100010], then get(1, 6) is 0b110001;
    With [0b11100010], then get(2, 6) is 0b111000;

    With [0b11100010], then get(0, 7) is 0b1100010;
    With [0b11100010], then get(1, 7) is 0b1110001;

    With [0b11100010], then get(0, 8) is 0b11100010;

    // OK. Now let's test getting bits from across byte boundaries.

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(0, 16) is 0b1111111101010101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(1, 16) is 0b0111111110101010;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(2, 16) is 0b0011111111010101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(3, 16) is 0b0001111111101010;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(4, 16) is 0b0000111111110101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(5, 16) is 0b0000011111111010;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(6, 16) is 0b0000001111111101;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(7, 16) is 0b0000000111111110;

    With [0b01010101, 0b11111111, 0b00000000, 0b11111111],
    then get(8, 16) is 0b0000000011111111;
}

macro_rules! bitfield_unit_set {
    (
        $(
            set($start:expr, $len:expr, $val:expr) is $expected:expr;
        )*
    ) => {
        #[test]
        fn bitfield_unit_set() {
            $(
                let mut unit = __BindgenBitfieldUnit::<[u8; 4]>::new([0, 0, 0, 0]);
                unit.set($start, $len, $val);
                let actual = unit.get(0, 32);

                println!();
                println!("set({}, {}, {:032b}", $start, $len, $val);
                println!("expected = {:064b}", $expected);
                println!("actual   = {actual:064b}");

                assert_eq!($expected, actual);
            )*
        }
    }
}

bitfield_unit_set! {
    // Once again, let's exhaustively test single byte combinations.

    set(0, 1, 0b11111111) is 0b00000001;
    set(1, 1, 0b11111111) is 0b00000010;
    set(2, 1, 0b11111111) is 0b00000100;
    set(3, 1, 0b11111111) is 0b00001000;
    set(4, 1, 0b11111111) is 0b00010000;
    set(5, 1, 0b11111111) is 0b00100000;
    set(6, 1, 0b11111111) is 0b01000000;
    set(7, 1, 0b11111111) is 0b10000000;

    set(0, 2, 0b11111111) is 0b00000011;
    set(1, 2, 0b11111111) is 0b00000110;
    set(2, 2, 0b11111111) is 0b00001100;
    set(3, 2, 0b11111111) is 0b00011000;
    set(4, 2, 0b11111111) is 0b00110000;
    set(5, 2, 0b11111111) is 0b01100000;
    set(6, 2, 0b11111111) is 0b11000000;

    set(0, 3, 0b11111111) is 0b00000111;
    set(1, 3, 0b11111111) is 0b00001110;
    set(2, 3, 0b11111111) is 0b00011100;
    set(3, 3, 0b11111111) is 0b00111000;
    set(4, 3, 0b11111111) is 0b01110000;
    set(5, 3, 0b11111111) is 0b11100000;

    set(0, 4, 0b11111111) is 0b00001111;
    set(1, 4, 0b11111111) is 0b00011110;
    set(2, 4, 0b11111111) is 0b00111100;
    set(3, 4, 0b11111111) is 0b01111000;
    set(4, 4, 0b11111111) is 0b11110000;

    set(0, 5, 0b11111111) is 0b00011111;
    set(1, 5, 0b11111111) is 0b00111110;
    set(2, 5, 0b11111111) is 0b01111100;
    set(3, 5, 0b11111111) is 0b11111000;

    set(0, 6, 0b11111111) is 0b00111111;
    set(1, 6, 0b11111111) is 0b01111110;
    set(2, 6, 0b11111111) is 0b11111100;

    set(0, 7, 0b11111111) is 0b01111111;
    set(1, 7, 0b11111111) is 0b11111110;

    set(0, 8, 0b11111111) is 0b11111111;

    // And, now let's cross byte boundaries.

    set(0, 16, 0b1111111111111111) is 0b00000000000000001111111111111111;
    set(1, 16, 0b1111111111111111) is 0b00000000000000011111111111111110;
    set(2, 16, 0b1111111111111111) is 0b00000000000000111111111111111100;
    set(3, 16, 0b1111111111111111) is 0b00000000000001111111111111111000;
    set(4, 16, 0b1111111111111111) is 0b00000000000011111111111111110000;
    set(5, 16, 0b1111111111111111) is 0b00000000000111111111111111100000;
    set(6, 16, 0b1111111111111111) is 0b00000000001111111111111111000000;
    set(7, 16, 0b1111111111111111) is 0b00000000011111111111111110000000;
    set(8, 16, 0b1111111111111111) is 0b00000000111111111111111100000000;
}

// Tests for const-generic methods
#[test]
fn bitfield_unit_get_const_matches_get() {
    // Test that get_const produces same results as get
    let unit = __BindgenBitfieldUnit::<[u8; 4]>::new([
        0b01010101, 0b11111111, 0b00000000, 0b11111111,
    ]);

    // Single byte tests
    assert_eq!(unit.get_const::<0, 1>(), unit.get(0, 1));
    assert_eq!(unit.get_const::<1, 1>(), unit.get(1, 1));
    assert_eq!(unit.get_const::<0, 8>(), unit.get(0, 8));
    assert_eq!(unit.get_const::<3, 5>(), unit.get(3, 5));

    // Cross-byte boundary tests
    assert_eq!(unit.get_const::<0, 16>(), unit.get(0, 16));
    assert_eq!(unit.get_const::<4, 16>(), unit.get(4, 16));
    assert_eq!(unit.get_const::<7, 16>(), unit.get(7, 16));
    assert_eq!(unit.get_const::<8, 16>(), unit.get(8, 16));

    // Large field
    assert_eq!(unit.get_const::<0, 32>(), unit.get(0, 32));
}

#[test]
fn bitfield_unit_set_const_matches_set() {
    // Test that set_const produces same results as set
    let test_value = 0b101010101010;

    for offset in [0, 1, 3, 7, 8, 12] {
        for width in [1, 2, 5, 8, 12] {
            let mut unit_const = __BindgenBitfieldUnit::<[u8; 4]>::new([0; 4]);
            let mut unit_runtime =
                __BindgenBitfieldUnit::<[u8; 4]>::new([0; 4]);

            match (offset, width) {
                (0, 1) => unit_const.set_const::<0, 1>(test_value),
                (0, 2) => unit_const.set_const::<0, 2>(test_value),
                (0, 5) => unit_const.set_const::<0, 5>(test_value),
                (0, 8) => unit_const.set_const::<0, 8>(test_value),
                (0, 12) => unit_const.set_const::<0, 12>(test_value),
                (1, 1) => unit_const.set_const::<1, 1>(test_value),
                (1, 2) => unit_const.set_const::<1, 2>(test_value),
                (1, 5) => unit_const.set_const::<1, 5>(test_value),
                (1, 8) => unit_const.set_const::<1, 8>(test_value),
                (1, 12) => unit_const.set_const::<1, 12>(test_value),
                (3, 1) => unit_const.set_const::<3, 1>(test_value),
                (3, 2) => unit_const.set_const::<3, 2>(test_value),
                (3, 5) => unit_const.set_const::<3, 5>(test_value),
                (3, 8) => unit_const.set_const::<3, 8>(test_value),
                (3, 12) => unit_const.set_const::<3, 12>(test_value),
                (7, 1) => unit_const.set_const::<7, 1>(test_value),
                (7, 2) => unit_const.set_const::<7, 2>(test_value),
                (7, 5) => unit_const.set_const::<7, 5>(test_value),
                (7, 8) => unit_const.set_const::<7, 8>(test_value),
                (7, 12) => unit_const.set_const::<7, 12>(test_value),
                (8, 1) => unit_const.set_const::<8, 1>(test_value),
                (8, 2) => unit_const.set_const::<8, 2>(test_value),
                (8, 5) => unit_const.set_const::<8, 5>(test_value),
                (8, 8) => unit_const.set_const::<8, 8>(test_value),
                (8, 12) => unit_const.set_const::<8, 12>(test_value),
                (12, 1) => unit_const.set_const::<12, 1>(test_value),
                (12, 2) => unit_const.set_const::<12, 2>(test_value),
                (12, 5) => unit_const.set_const::<12, 5>(test_value),
                (12, 8) => unit_const.set_const::<12, 8>(test_value),
                (12, 12) => unit_const.set_const::<12, 12>(test_value),
                _ => continue,
            }

            unit_runtime.set(offset, width, test_value);
            // Compare by reading back the full value
            assert_eq!(unit_const.get(0, 32), unit_runtime.get(0, 32));
        }
    }
}

#[test]
fn bitfield_unit_raw_const_methods() {
    let unit = __BindgenBitfieldUnit::<[u8; 2]>::new([0b10011101, 0b00011101]);

    // Test raw_get_const
    unsafe {
        assert_eq!(
            __BindgenBitfieldUnit::raw_get_const::<0, 8>(&unit),
            unit.get(0, 8)
        );
        assert_eq!(
            __BindgenBitfieldUnit::raw_get_const::<4, 8>(&unit),
            unit.get(4, 8)
        );
        assert_eq!(
            __BindgenBitfieldUnit::raw_get_const::<0, 16>(&unit),
            unit.get(0, 16)
        );
    }

    // Test raw_set_const
    let mut unit_const = __BindgenBitfieldUnit::<[u8; 2]>::new([0; 2]);
    let mut unit_runtime = __BindgenBitfieldUnit::<[u8; 2]>::new([0; 2]);

    unsafe {
        __BindgenBitfieldUnit::raw_set_const::<3, 5>(&mut unit_const, 0b11111);
    }
    unit_runtime.set(3, 5, 0b11111);

    // Compare by reading back
    assert_eq!(unit_const.get(0, 16), unit_runtime.get(0, 16));
}

// Regression: const-generic accessors must not shift-overflow when
// BIT_WIDTH equals the native word size (usize::BITS). Previously
// `(1usize << BIT_WIDTH) - 1` panicked in debug builds for 64-bit
// fields on 64-bit targets (and 32-bit fields on 32-bit targets).
#[test]
fn bitfield_unit_const_full_word_width() {
    let storage = [0x12u8, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0];
    let unit = __BindgenBitfieldUnit::<[u8; 8]>::new(storage);

    // Width == 64 on 64-bit hosts, and width == 32 exercises the
    // boundary on 32-bit hosts (still safe on 64-bit, just routes
    // through a different branch).
    assert_eq!(unit.get_const::<0, 64>(), unit.get(0, 64));
    assert_eq!(unit.get_const::<0, 32>(), unit.get(0, 32));

    unsafe {
        assert_eq!(
            __BindgenBitfieldUnit::raw_get_const::<0, 64>(&unit),
            unit.get(0, 64),
        );
    }

    let mut unit_const = __BindgenBitfieldUnit::<[u8; 8]>::new([0; 8]);
    let mut unit_runtime = __BindgenBitfieldUnit::<[u8; 8]>::new([0; 8]);
    let value = 0xDEAD_BEEF_CAFE_BABE_u64;

    unit_const.set_const::<0, 64>(value);
    unit_runtime.set(0, 64, value);
    assert_eq!(unit_const.get(0, 64), unit_runtime.get(0, 64));

    let mut unit_raw = __BindgenBitfieldUnit::<[u8; 8]>::new([0; 8]);
    unsafe {
        __BindgenBitfieldUnit::raw_set_const::<0, 64>(&mut unit_raw, value);
    }
    assert_eq!(unit_raw.get(0, 64), value);

    // Exercise the new `field_mask = !0 << bit_shift` branch with a
    // non-zero bit_shift (BIT_WIDTH + bit_shift == usize::BITS but
    // BIT_WIDTH < usize::BITS, so the value-mask still runs).
    let mut unit_shifted_const =
        __BindgenBitfieldUnit::<[u8; 9]>::new([0xAA; 9]);
    let mut unit_shifted_runtime =
        __BindgenBitfieldUnit::<[u8; 9]>::new([0xAA; 9]);
    unit_shifted_const.set_const::<1, 63>(value & ((1u64 << 63) - 1));
    unit_shifted_runtime.set(1, 63, value & ((1u64 << 63) - 1));
    assert_eq!(
        unit_shifted_const.get(0, 64),
        unit_shifted_runtime.get(0, 64),
    );
    assert_eq!(
        unit_shifted_const.get_const::<1, 63>(),
        unit_shifted_runtime.get(1, 63),
    );

    let mut unit_shifted_raw = __BindgenBitfieldUnit::<[u8; 9]>::new([0xAA; 9]);
    let mut unit_shifted_raw_runtime =
        __BindgenBitfieldUnit::<[u8; 9]>::new([0xAA; 9]);
    unsafe {
        __BindgenBitfieldUnit::raw_set_const::<1, 63>(
            &mut unit_shifted_raw,
            value & ((1u64 << 63) - 1),
        );
    }
    unit_shifted_raw_runtime.set(1, 63, value & ((1u64 << 63) - 1));
    assert_eq!(
        unit_shifted_raw.get(0, 64),
        unit_shifted_raw_runtime.get(0, 64),
    );
}