Skip to content

Commit

Permalink
more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Jan 13, 2025
1 parent be824ae commit 2d7dbf6
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hs-bindgen/examples/bitfields.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ struct overflow64 {
long x : 33;
long y : 33;
};

// alignment
struct alignA {
unsigned char x : 1;
int y : 10;
};

struct alignB {
unsigned char x : 7;
int y : 31;
};
125 changes: 125 additions & 0 deletions hs-bindgen/fixtures/bitfields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,128 @@ impl overflow64 {
__bindgen_bitfield_unit
}
}
#[repr(C)]
#[repr(align(4))]
#[derive(Debug, Copy, Clone)]
pub struct alignA {
pub _bitfield_align_1: [u16; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 2usize]>,
pub __bindgen_padding_0: u16,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
["Size of alignA"][::std::mem::size_of::<alignA>() - 4usize];
["Alignment of alignA"][::std::mem::align_of::<alignA>() - 4usize];
};
impl alignA {
#[inline]
pub fn x(&self) -> ::std::os::raw::c_uchar {
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u8) }
}
#[inline]
pub fn set_x(&mut self, val: ::std::os::raw::c_uchar) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self._bitfield_1.set(0usize, 1u8, val as u64)
}
}
#[inline]
pub fn y(&self) -> ::std::os::raw::c_int {
unsafe { ::std::mem::transmute(self._bitfield_1.get(1usize, 10u8) as u32) }
}
#[inline]
pub fn set_y(&mut self, val: ::std::os::raw::c_int) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(1usize, 10u8, val as u64)
}
}
#[inline]
pub fn new_bitfield_1(
x: ::std::os::raw::c_uchar,
y: ::std::os::raw::c_int,
) -> __BindgenBitfieldUnit<[u8; 2usize]> {
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 2usize]> = Default::default();
__bindgen_bitfield_unit
.set(
0usize,
1u8,
{
let x: u8 = unsafe { ::std::mem::transmute(x) };
x as u64
},
);
__bindgen_bitfield_unit
.set(
1usize,
10u8,
{
let y: u32 = unsafe { ::std::mem::transmute(y) };
y as u64
},
);
__bindgen_bitfield_unit
}
}
#[repr(C)]
#[repr(align(8))]
#[derive(Debug, Copy, Clone)]
pub struct alignB {
pub _bitfield_align_1: [u32; 0],
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 5usize]>,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
["Size of alignB"][::std::mem::size_of::<alignB>() - 8usize];
["Alignment of alignB"][::std::mem::align_of::<alignB>() - 8usize];
};
impl alignB {
#[inline]
pub fn x(&self) -> ::std::os::raw::c_uchar {
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 7u8) as u8) }
}
#[inline]
pub fn set_x(&mut self, val: ::std::os::raw::c_uchar) {
unsafe {
let val: u8 = ::std::mem::transmute(val);
self._bitfield_1.set(0usize, 7u8, val as u64)
}
}
#[inline]
pub fn y(&self) -> ::std::os::raw::c_long {
unsafe { ::std::mem::transmute(self._bitfield_1.get(7usize, 31u8) as u64) }
}
#[inline]
pub fn set_y(&mut self, val: ::std::os::raw::c_long) {
unsafe {
let val: u64 = ::std::mem::transmute(val);
self._bitfield_1.set(7usize, 31u8, val as u64)
}
}
#[inline]
pub fn new_bitfield_1(
x: ::std::os::raw::c_uchar,
y: ::std::os::raw::c_long,
) -> __BindgenBitfieldUnit<[u8; 5usize]> {
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 5usize]> = Default::default();
__bindgen_bitfield_unit
.set(
0usize,
7u8,
{
let x: u8 = unsafe { ::std::mem::transmute(x) };
x as u64
},
);
__bindgen_bitfield_unit
.set(
7usize,
31u8,
{
let y: u64 = unsafe { ::std::mem::transmute(y) };
y as u64
},
);
__bindgen_bitfield_unit
}
}

0 comments on commit 2d7dbf6

Please sign in to comment.