| 1 |
//! Macros for checking that types have the same layout as other types. |
| 2 |
|
| 3 |
#![allow(unused_macros)] |
| 4 |
|
| 5 |
/// Check that the size and alignment of a type match the `sys` bindings. |
| 6 |
macro_rules! check_type { |
| 7 |
($struct:ident) => { |
| 8 |
assert_eq_size!($struct, c::$struct); |
| 9 |
assert_eq_align!($struct, c::$struct); |
| 10 |
}; |
| 11 |
} |
| 12 |
|
| 13 |
/// The same as `check_type`, but for unions and anonymous structs we've |
| 14 |
/// renamed to avoid having types like `bindgen_ty_1` in the API. |
| 15 |
macro_rules! check_renamed_type { |
| 16 |
($to:ident, $from:ident) => { |
| 17 |
assert_eq_size!($to, c::$from); |
| 18 |
assert_eq_align!($to, c::$from); |
| 19 |
}; |
| 20 |
} |
| 21 |
|
| 22 |
/// Check that the field of a struct has the same offset as the corresponding |
| 23 |
/// field in the `sys` bindings. |
| 24 |
macro_rules! check_struct_field { |
| 25 |
($struct:ident, $field:ident) => { |
| 26 |
const_assert_eq!( |
| 27 |
memoffset::offset_of!($struct, $field), |
| 28 |
memoffset::offset_of!(c::$struct, $field) |
| 29 |
); |
| 30 |
|
| 31 |
// This can't use `const_assert_eq` because `span_of` returns a |
| 32 |
// `Range`, which can't be compared in const contexts. |
| 33 |
assert_eq!( |
| 34 |
memoffset::span_of!($struct, $field), |
| 35 |
memoffset::span_of!(c::$struct, $field) |
| 36 |
); |
| 37 |
}; |
| 38 |
} |
| 39 |
|
| 40 |
/// The same as `check_struct_field`, but for unions and anonymous structs |
| 41 |
/// we've renamed to avoid having types like `bindgen_ty_1` in the API. |
| 42 |
macro_rules! check_struct_renamed_field { |
| 43 |
($struct:ident, $to:ident, $from:ident) => { |
| 44 |
const_assert_eq!( |
| 45 |
memoffset::offset_of!($struct, $to), |
| 46 |
memoffset::offset_of!(c::$struct, $from) |
| 47 |
); |
| 48 |
|
| 49 |
// As above, this can't use `const_assert_eq`. |
| 50 |
assert_eq!( |
| 51 |
memoffset::span_of!($struct, $to), |
| 52 |
memoffset::span_of!(c::$struct, $from) |
| 53 |
); |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
/// The same as `check_struct_field`, but for when the struct is renamed |
| 58 |
/// but the field is not. |
| 59 |
macro_rules! check_renamed_struct_field { |
| 60 |
($to_struct:ident, $from_struct:ident, $field:ident) => { |
| 61 |
const_assert_eq!( |
| 62 |
memoffset::offset_of!($to_struct, $field), |
| 63 |
memoffset::offset_of!(c::$from_struct, $field) |
| 64 |
); |
| 65 |
|
| 66 |
// As above, this can't use `const_assert_eq`. |
| 67 |
assert_eq!( |
| 68 |
memoffset::span_of!($to_struct, $field), |
| 69 |
memoffset::span_of!(c::$from_struct, $field) |
| 70 |
); |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
/// The same as `check_struct_renamed_field`, but for when both the struct and |
| 75 |
/// a field are renamed. |
| 76 |
macro_rules! check_renamed_struct_renamed_field { |
| 77 |
($to_struct:ident, $from_struct:ident, $to:ident, $from:ident) => { |
| 78 |
const_assert_eq!( |
| 79 |
memoffset::offset_of!($to_struct, $to), |
| 80 |
memoffset::offset_of!(c::$from_struct, $from) |
| 81 |
); |
| 82 |
|
| 83 |
// As above, this can't use `const_assert_eq`. |
| 84 |
assert_eq!( |
| 85 |
memoffset::span_of!($to_struct, $to), |
| 86 |
memoffset::span_of!(c::$from_struct, $from) |
| 87 |
); |
| 88 |
}; |
| 89 |
} |
| 90 |
|
| 91 |
/// For the common case of no renaming, check all fields of a struct. |
| 92 |
macro_rules! check_struct { |
| 93 |
($name:ident, $($field:ident),*) => { |
| 94 |
// Check the size and alignment. |
| 95 |
check_type!($name); |
| 96 |
|
| 97 |
// Check that we have all the fields. |
| 98 |
if false { |
| 99 |
#[allow(unreachable_code)] |
| 100 |
let _test = $name { |
| 101 |
$($field: panic!()),* |
| 102 |
}; |
| 103 |
#[allow(unreachable_code)] |
| 104 |
let _test = c::$name { |
| 105 |
$($field: panic!()),* |
| 106 |
}; |
| 107 |
} |
| 108 |
|
| 109 |
// Check that the fields have the right sizes and offsets. |
| 110 |
$(check_struct_field!($name, $field));* |
| 111 |
}; |
| 112 |
} |
| 113 |
|
| 114 |
/// For the case of renaming, check all fields of a struct. |
| 115 |
macro_rules! check_renamed_struct { |
| 116 |
($to_struct:ident, $from_struct:ident, $($field:ident),*) => { |
| 117 |
// Check the size and alignment. |
| 118 |
check_renamed_type!($to_struct, $from_struct); |
| 119 |
|
| 120 |
// Check that we have all the fields. |
| 121 |
if false { |
| 122 |
#[allow(unreachable_code)] |
| 123 |
let _test = $to_struct { |
| 124 |
$($field: panic!()),* |
| 125 |
}; |
| 126 |
#[allow(unreachable_code)] |
| 127 |
let _test = c::$from_struct { |
| 128 |
$($field: panic!()),* |
| 129 |
}; |
| 130 |
} |
| 131 |
|
| 132 |
// Check that the fields have the right sizes and offsets. |
| 133 |
$(check_renamed_struct_field!($to_struct, $from_struct, $field));* |
| 134 |
}; |
| 135 |
} |