Skip to content

Commit aa1030e

Browse files
chrysnemilio
authored andcommitted
Tests: Add a --use-core --impl-debug case
This demonstrates the [...] abbreviation of large arrays in contrast to the std variety. (Long (>32) arrays currently don't have a dedicated test, but are piggy-backed on the derive-debug-bitfield and derive-debug-function-pointer tests). Note that the single occurrence of the `std` namespace in the expectation is valid: No --ctypes-prefix was set, and ::std::os::raw is the default value that needs to be overridden in most practical --use-core applications.
1 parent b3fca4d commit aa1030e

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[repr(C)]
11+
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
12+
pub struct __BindgenBitfieldUnit<Storage, Align>
13+
where
14+
Storage: AsRef<[u8]> + AsMut<[u8]>,
15+
{
16+
storage: Storage,
17+
align: [Align; 0],
18+
}
19+
20+
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align>
21+
where
22+
Storage: AsRef<[u8]> + AsMut<[u8]>,
23+
{
24+
#[inline]
25+
pub fn new(storage: Storage) -> Self {
26+
Self { storage, align: [] }
27+
}
28+
29+
#[inline]
30+
pub fn get_bit(&self, index: usize) -> bool {
31+
debug_assert!(index / 8 < self.storage.as_ref().len());
32+
33+
let byte_index = index / 8;
34+
let byte = self.storage.as_ref()[byte_index];
35+
36+
let bit_index = if cfg!(target_endian = "big") {
37+
7 - (index % 8)
38+
} else {
39+
index % 8
40+
};
41+
42+
let mask = 1 << bit_index;
43+
44+
byte & mask == mask
45+
}
46+
47+
#[inline]
48+
pub fn set_bit(&mut self, index: usize, val: bool) {
49+
debug_assert!(index / 8 < self.storage.as_ref().len());
50+
51+
let byte_index = index / 8;
52+
let byte = &mut self.storage.as_mut()[byte_index];
53+
54+
let bit_index = if cfg!(target_endian = "big") {
55+
7 - (index % 8)
56+
} else {
57+
index % 8
58+
};
59+
60+
let mask = 1 << bit_index;
61+
if val {
62+
*byte |= mask;
63+
} else {
64+
*byte &= !mask;
65+
}
66+
}
67+
68+
#[inline]
69+
pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 {
70+
debug_assert!(bit_width <= 64);
71+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
72+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
73+
74+
let mut val = 0;
75+
76+
for i in 0..(bit_width as usize) {
77+
if self.get_bit(i + bit_offset) {
78+
let index = if cfg!(target_endian = "big") {
79+
bit_width as usize - 1 - i
80+
} else {
81+
i
82+
};
83+
val |= 1 << index;
84+
}
85+
}
86+
87+
val
88+
}
89+
90+
#[inline]
91+
pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) {
92+
debug_assert!(bit_width <= 64);
93+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
94+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
95+
96+
for i in 0..(bit_width as usize) {
97+
let mask = 1 << i;
98+
let val_bit_is_set = val & mask == mask;
99+
let index = if cfg!(target_endian = "big") {
100+
bit_width as usize - 1 - i
101+
} else {
102+
i
103+
};
104+
self.set_bit(index + bit_offset, val_bit_is_set);
105+
}
106+
}
107+
}
108+
#[repr(C)]
109+
#[derive(Copy, Clone)]
110+
pub struct C {
111+
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
112+
pub large_array: [::std::os::raw::c_int; 50usize],
113+
}
114+
#[test]
115+
fn bindgen_test_layout_C() {
116+
assert_eq!(
117+
::core::mem::size_of::<C>(),
118+
204usize,
119+
concat!("Size of: ", stringify!(C))
120+
);
121+
assert_eq!(
122+
::core::mem::align_of::<C>(),
123+
4usize,
124+
concat!("Alignment of ", stringify!(C))
125+
);
126+
assert_eq!(
127+
unsafe { &(*(::core::ptr::null::<C>())).large_array as *const _ as usize },
128+
4usize,
129+
concat!(
130+
"Offset of field: ",
131+
stringify!(C),
132+
"::",
133+
stringify!(large_array)
134+
)
135+
);
136+
}
137+
impl Default for C {
138+
fn default() -> Self {
139+
unsafe { ::core::mem::zeroed() }
140+
}
141+
}
142+
impl ::core::fmt::Debug for C {
143+
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
144+
write!(
145+
f,
146+
"C {{ a : {:?}, b : {:?}, large_array: [...] }}",
147+
self.a(),
148+
self.b()
149+
)
150+
}
151+
}
152+
impl C {
153+
#[inline]
154+
pub fn a(&self) -> bool {
155+
unsafe { ::core::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u8) }
156+
}
157+
#[inline]
158+
pub fn set_a(&mut self, val: bool) {
159+
unsafe {
160+
let val: u8 = ::core::mem::transmute(val);
161+
self._bitfield_1.set(0usize, 1u8, val as u64)
162+
}
163+
}
164+
#[inline]
165+
pub fn b(&self) -> bool {
166+
unsafe { ::core::mem::transmute(self._bitfield_1.get(1usize, 7u8) as u8) }
167+
}
168+
#[inline]
169+
pub fn set_b(&mut self, val: bool) {
170+
unsafe {
171+
let val: u8 = ::core::mem::transmute(val);
172+
self._bitfield_1.set(1usize, 7u8, val as u64)
173+
}
174+
}
175+
#[inline]
176+
pub fn new_bitfield_1(a: bool, b: bool) -> __BindgenBitfieldUnit<[u8; 1usize], u8> {
177+
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 1usize], u8> =
178+
Default::default();
179+
__bindgen_bitfield_unit.set(0usize, 1u8, {
180+
let a: u8 = unsafe { ::core::mem::transmute(a) };
181+
a as u64
182+
});
183+
__bindgen_bitfield_unit.set(1usize, 7u8, {
184+
let b: u8 = unsafe { ::core::mem::transmute(b) };
185+
b as u64
186+
});
187+
__bindgen_bitfield_unit
188+
}
189+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --impl-debug --use-core
2+
3+
class C {
4+
bool a: 1;
5+
bool b: 7;
6+
int large_array[50];
7+
};

0 commit comments

Comments
 (0)