Skip to content

Commit

Permalink
codegen: Don't check large_arrays to generate bitfield ctors because …
Browse files Browse the repository at this point in the history
…they use the Default trait.

Fixes #2082
  • Loading branch information
emilio committed Jul 25, 2021
1 parent 6714007 commit 271eeb0
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,9 +1502,11 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
let mut ctor_impl = quote! {};

// We cannot generate any constructor if the underlying storage can't
// implement AsRef<[u8]> / AsMut<[u8]> / etc.
let mut generate_ctor = layout.size <= RUST_DERIVE_IN_ARRAY_LIMIT ||
ctx.options().rust_features().larger_arrays;
// implement AsRef<[u8]> / AsMut<[u8]> / etc, or can't derive Default.
//
// We don't check `larger_arrays` here because Default does still have
// the 32 items limitation.
let mut generate_ctor = layout.size <= RUST_DERIVE_IN_ARRAY_LIMIT;

let mut access_spec = !fields_should_be_private;
for bf in self.bitfields() {
Expand Down
149 changes: 147 additions & 2 deletions tests/expectations/tests/timex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
}
}
#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub struct timex {
pub tai: ::std::os::raw::c_int,
pub _bitfield_align_1: [u8; 0],
Expand Down Expand Up @@ -131,7 +131,7 @@ impl Default for timex {
}
}
#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub struct timex_named {
pub tai: ::std::os::raw::c_int,
pub _bitfield_align_1: [u32; 0],
Expand Down Expand Up @@ -171,3 +171,148 @@ impl Default for timex_named {
}
}
}
impl timex_named {
#[inline]
pub fn a(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(0usize, 32u8) as u32)
}
}
#[inline]
pub fn set_a(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(0usize, 32u8, val as u64)
}
}
#[inline]
pub fn b(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(32usize, 32u8) as u32)
}
}
#[inline]
pub fn set_b(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(32usize, 32u8, val as u64)
}
}
#[inline]
pub fn c(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(64usize, 32u8) as u32)
}
}
#[inline]
pub fn set_c(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(64usize, 32u8, val as u64)
}
}
#[inline]
pub fn d(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(96usize, 32u8) as u32)
}
}
#[inline]
pub fn set_d(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(96usize, 32u8, val as u64)
}
}
#[inline]
pub fn e(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(128usize, 32u8) as u32)
}
}
#[inline]
pub fn set_e(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(128usize, 32u8, val as u64)
}
}
#[inline]
pub fn f(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(160usize, 32u8) as u32)
}
}
#[inline]
pub fn set_f(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(160usize, 32u8, val as u64)
}
}
#[inline]
pub fn g(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(192usize, 32u8) as u32)
}
}
#[inline]
pub fn set_g(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(192usize, 32u8, val as u64)
}
}
#[inline]
pub fn h(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(224usize, 32u8) as u32)
}
}
#[inline]
pub fn set_h(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(224usize, 32u8, val as u64)
}
}
#[inline]
pub fn i(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(256usize, 32u8) as u32)
}
}
#[inline]
pub fn set_i(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(256usize, 32u8, val as u64)
}
}
#[inline]
pub fn j(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(288usize, 32u8) as u32)
}
}
#[inline]
pub fn set_j(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(288usize, 32u8, val as u64)
}
}
#[inline]
pub fn k(&self) -> ::std::os::raw::c_int {
unsafe {
::std::mem::transmute(self._bitfield_1.get(320usize, 32u8) as u32)
}
}
#[inline]
pub fn set_k(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(320usize, 32u8, val as u64)
}
}
}
2 changes: 0 additions & 2 deletions tests/headers/timex.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// bindgen-flags: --rust-target 1.40

struct timex {
int tai;

Expand Down

0 comments on commit 271eeb0

Please sign in to comment.