diff --git a/bindgen-tests/tests/expectations/tests/bitfield-32bit-overflow.rs b/bindgen-tests/tests/expectations/tests/bitfield-32bit-overflow.rs index 7125be5607..81ce12a5c9 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield-32bit-overflow.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield-32bit-overflow.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/bitfield-large.rs b/bindgen-tests/tests/expectations/tests/bitfield-large.rs index 47afa9a3ba..8024d88c3b 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield-large.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield-large.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/bitfield-linux-32.rs b/bindgen-tests/tests/expectations/tests/bitfield-linux-32.rs index 075aa27e5e..8b0b37b481 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield-linux-32.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield-linux-32.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs b/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs index 25cc182929..84e152f3fd 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/bitfield_align.rs b/bindgen-tests/tests/expectations/tests/bitfield_align.rs index c1c72f3132..5f1321bbb1 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield_align.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield_align.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs b/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs index 0f783fe76e..2a676d5636 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs @@ -36,8 +36,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -61,9 +63,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -94,7 +98,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -138,7 +142,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/bitfield_method_mangling.rs b/bindgen-tests/tests/expectations/tests/bitfield_method_mangling.rs index 966943f935..1a08cd00f9 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield_method_mangling.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield_method_mangling.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/bitfield_pragma_packed.rs b/bindgen-tests/tests/expectations/tests/bitfield_pragma_packed.rs index 0cd2002fb6..8869593ed5 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield_pragma_packed.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield_pragma_packed.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/default_visibility_crate.rs b/bindgen-tests/tests/expectations/tests/default_visibility_crate.rs index 0aca5a3b8a..aeb6a717f6 100644 --- a/bindgen-tests/tests/expectations/tests/default_visibility_crate.rs +++ b/bindgen-tests/tests/expectations/tests/default_visibility_crate.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/default_visibility_private.rs b/bindgen-tests/tests/expectations/tests/default_visibility_private.rs index 0d4d42cfdb..4ee26721b0 100644 --- a/bindgen-tests/tests/expectations/tests/default_visibility_private.rs +++ b/bindgen-tests/tests/expectations/tests/default_visibility_private.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/default_visibility_private_respects_cxx_access_spec.rs b/bindgen-tests/tests/expectations/tests/default_visibility_private_respects_cxx_access_spec.rs index cf135cfd3d..e676f6470d 100644 --- a/bindgen-tests/tests/expectations/tests/default_visibility_private_respects_cxx_access_spec.rs +++ b/bindgen-tests/tests/expectations/tests/default_visibility_private_respects_cxx_access_spec.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/derive-debug-bitfield-1-51.rs b/bindgen-tests/tests/expectations/tests/derive-debug-bitfield-1-51.rs index fa007056a1..351d3b31e5 100644 --- a/bindgen-tests/tests/expectations/tests/derive-debug-bitfield-1-51.rs +++ b/bindgen-tests/tests/expectations/tests/derive-debug-bitfield-1-51.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/divide-by-zero-in-struct-layout.rs b/bindgen-tests/tests/expectations/tests/divide-by-zero-in-struct-layout.rs index 0e1fe567ac..e01a9b3c72 100644 --- a/bindgen-tests/tests/expectations/tests/divide-by-zero-in-struct-layout.rs +++ b/bindgen-tests/tests/expectations/tests/divide-by-zero-in-struct-layout.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/field-visibility-callback.rs b/bindgen-tests/tests/expectations/tests/field-visibility-callback.rs index 8634dafba1..aebc04f03b 100644 --- a/bindgen-tests/tests/expectations/tests/field-visibility-callback.rs +++ b/bindgen-tests/tests/expectations/tests/field-visibility-callback.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/field-visibility.rs b/bindgen-tests/tests/expectations/tests/field-visibility.rs index 5dfe7502d3..3e43755a57 100644 --- a/bindgen-tests/tests/expectations/tests/field-visibility.rs +++ b/bindgen-tests/tests/expectations/tests/field-visibility.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs b/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs index 6e420e9fc3..6e9f6e7753 100644 --- a/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs +++ b/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/issue-1034.rs b/bindgen-tests/tests/expectations/tests/issue-1034.rs index 75e3ed3858..1034520c48 100644 --- a/bindgen-tests/tests/expectations/tests/issue-1034.rs +++ b/bindgen-tests/tests/expectations/tests/issue-1034.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs b/bindgen-tests/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs index 7d517e5633..3d14c81a77 100644 --- a/bindgen-tests/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs +++ b/bindgen-tests/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/issue-1947.rs b/bindgen-tests/tests/expectations/tests/issue-1947.rs index bec383bbdb..cceb42d8c5 100644 --- a/bindgen-tests/tests/expectations/tests/issue-1947.rs +++ b/bindgen-tests/tests/expectations/tests/issue-1947.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs b/bindgen-tests/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs index 7fbc89b21c..4e79cfdf71 100644 --- a/bindgen-tests/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs +++ b/bindgen-tests/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs @@ -36,8 +36,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -61,9 +63,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -94,7 +98,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -138,7 +142,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/issue-816.rs b/bindgen-tests/tests/expectations/tests/issue-816.rs index 9ee600fd74..56e719238b 100644 --- a/bindgen-tests/tests/expectations/tests/issue-816.rs +++ b/bindgen-tests/tests/expectations/tests/issue-816.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs index c8f406db19..7dd23241e9 100644 --- a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs +++ b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/layout_align.rs b/bindgen-tests/tests/expectations/tests/layout_align.rs index 906c26d57e..18662d4c83 100644 --- a/bindgen-tests/tests/expectations/tests/layout_align.rs +++ b/bindgen-tests/tests/expectations/tests/layout_align.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/layout_mbuf.rs b/bindgen-tests/tests/expectations/tests/layout_mbuf.rs index 47ea51d2c2..cb3698812b 100644 --- a/bindgen-tests/tests/expectations/tests/layout_mbuf.rs +++ b/bindgen-tests/tests/expectations/tests/layout_mbuf.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/only_bitfields.rs b/bindgen-tests/tests/expectations/tests/only_bitfields.rs index fe317dc126..3aedce1e3f 100644 --- a/bindgen-tests/tests/expectations/tests/only_bitfields.rs +++ b/bindgen-tests/tests/expectations/tests/only_bitfields.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/packed-bitfield.rs b/bindgen-tests/tests/expectations/tests/packed-bitfield.rs index 6aa59ec8a3..4e3918f558 100644 --- a/bindgen-tests/tests/expectations/tests/packed-bitfield.rs +++ b/bindgen-tests/tests/expectations/tests/packed-bitfield.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/private_fields.rs b/bindgen-tests/tests/expectations/tests/private_fields.rs index 0614b7417f..a5d9e84499 100644 --- a/bindgen-tests/tests/expectations/tests/private_fields.rs +++ b/bindgen-tests/tests/expectations/tests/private_fields.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/redundant-packed-and-align.rs b/bindgen-tests/tests/expectations/tests/redundant-packed-and-align.rs index 43d88df698..c01762ca98 100644 --- a/bindgen-tests/tests/expectations/tests/redundant-packed-and-align.rs +++ b/bindgen-tests/tests/expectations/tests/redundant-packed-and-align.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/struct_with_bitfields.rs b/bindgen-tests/tests/expectations/tests/struct_with_bitfields.rs index 3cdf6fce1c..23588ee7da 100644 --- a/bindgen-tests/tests/expectations/tests/struct_with_bitfields.rs +++ b/bindgen-tests/tests/expectations/tests/struct_with_bitfields.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/timex.rs b/bindgen-tests/tests/expectations/tests/timex.rs index 0c8391c76b..3d25971315 100644 --- a/bindgen-tests/tests/expectations/tests/timex.rs +++ b/bindgen-tests/tests/expectations/tests/timex.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/union_bitfield.rs b/bindgen-tests/tests/expectations/tests/union_bitfield.rs index e924801114..70da5c3b02 100644 --- a/bindgen-tests/tests/expectations/tests/union_bitfield.rs +++ b/bindgen-tests/tests/expectations/tests/union_bitfield.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield.rs b/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield.rs index 8be065da94..dd1a55e9b5 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen-tests/tests/expectations/tests/weird_bitfields.rs b/bindgen-tests/tests/expectations/tests/weird_bitfields.rs index b3f16242f8..17accb01d9 100644 --- a/bindgen-tests/tests/expectations/tests/weird_bitfields.rs +++ b/bindgen-tests/tests/expectations/tests/weird_bitfields.rs @@ -35,8 +35,10 @@ where pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { + *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) + }; Self::extract_bit(byte, index) } #[inline] @@ -60,9 +62,11 @@ where pub unsafe fn raw_set_bit(this: *mut Self, index: usize, val: bool) { debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); - *byte = Self::change_bit(*byte, index, val); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { @@ -93,7 +97,7 @@ where ); let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -137,7 +141,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } } diff --git a/bindgen/codegen/bitfield_unit_raw_ref_macros.rs b/bindgen/codegen/bitfield_unit_raw_ref_macros.rs index 3411c22eac..0c864c7369 100644 --- a/bindgen/codegen/bitfield_unit_raw_ref_macros.rs +++ b/bindgen/codegen/bitfield_unit_raw_ref_macros.rs @@ -43,8 +43,8 @@ where debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = *(core::ptr::addr_of!((*this).storage) as *const u8) - .offset(byte_index as isize); + let byte = unsafe { *(core::ptr::addr_of!((*this).storage) as *const u8) + .offset(byte_index as isize) }; Self::extract_bit(byte, index) } @@ -80,10 +80,12 @@ where debug_assert!(index / 8 < core::mem::size_of::()); let byte_index = index / 8; - let byte = (core::ptr::addr_of_mut!((*this).storage) as *mut u8) - .offset(byte_index as isize); + let byte = unsafe { + (core::ptr::addr_of_mut!((*this).storage) as *mut u8) + .offset(byte_index as isize) + }; - *byte = Self::change_bit(*byte, index, val); + unsafe { *byte = Self::change_bit(*byte, index, val) }; } #[inline] @@ -127,7 +129,7 @@ where let mut val = 0; for i in 0..(bit_width as usize) { - if Self::raw_get_bit(this, i + bit_offset) { + if unsafe { Self::raw_get_bit(this, i + bit_offset) } { let index = if cfg!(target_endian = "big") { bit_width as usize - 1 - i } else { @@ -183,7 +185,7 @@ where } else { i }; - Self::raw_set_bit(this, index + bit_offset, val_bit_is_set); + unsafe { Self::raw_set_bit(this, index + bit_offset, val_bit_is_set) }; } } }