Skip to content

Commit 0478a39

Browse files
jsgfemilio
authored andcommitted
Honour --wrap-unsafe-ops
Previously it was generating inner `unsafe` blocks for all unsafe functions in conformance with Rust 2024, but now only do it when `--wrap-unsafe-ops` is enabled for consistency with other generated code. Also rename `flex_mut_ref` -> `flex_ref_mut` to make it consistent with `flex_ptr_mut` and general Rust convention.
1 parent 0dae6d5 commit 0478a39

File tree

2 files changed

+74
-74
lines changed

2 files changed

+74
-74
lines changed

bindgen-tests/tests/expectations/tests/flexarray.rs

+51-61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen/codegen/mod.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,24 @@ impl CompInfo {
27272727
.rust_features()
27282728
.ptr_metadata
27292729
{
2730+
let flex_ref_inner = ctx.wrap_unsafe_ops(quote! {
2731+
Self::flex_ptr(self, len)
2732+
});
2733+
let flex_ref_mut_inner = ctx.wrap_unsafe_ops(quote! {
2734+
Self::flex_ptr_mut(self, len).assume_init()
2735+
});
2736+
let flex_ptr_inner = ctx.wrap_unsafe_ops(quote! {
2737+
&*::#prefix::ptr::from_raw_parts(ptr as *const (), len)
2738+
});
2739+
let flex_ptr_mut_inner = ctx.wrap_unsafe_ops(quote! {
2740+
// Initialize reference without ever exposing it, as its possibly uninitialized
2741+
let mut uninit = ::#prefix::mem::MaybeUninit::<&mut #dst_ty_for_impl>::uninit();
2742+
(uninit.as_mut_ptr() as *mut *mut #dst_ty_for_impl)
2743+
.write(::#prefix::ptr::from_raw_parts_mut(ptr as *mut (), len));
2744+
2745+
uninit
2746+
});
2747+
27302748
(
27312749
quote! {
27322750
pub fn fixed(&self) -> (& #sized_ty_for_impl, usize) {
@@ -2740,7 +2758,6 @@ impl CompInfo {
27402758
unsafe {
27412759
let (ptr, len) = (self as *mut Self).to_raw_parts();
27422760
(&mut *(ptr as *mut #sized_ty_for_impl), len)
2743-
27442761
}
27452762
}
27462763
},
@@ -2750,23 +2767,23 @@ impl CompInfo {
27502767
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
27512768
pub unsafe fn flex_ref(&self, len: usize) -> &#dst_ty_for_impl {
27522769
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2753-
unsafe { Self::flex_ptr(self, len) }
2770+
#flex_ref_inner
27542771
}
27552772

27562773
/// Convert a mutable sized prefix to an unsized structure with the given length.
27572774
///
27582775
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
2759-
pub unsafe fn flex_mut_ref(&mut self, len: usize) -> &mut #dst_ty_for_impl {
2776+
pub unsafe fn flex_ref_mut(&mut self, len: usize) -> &mut #dst_ty_for_impl {
27602777
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2761-
unsafe { Self::flex_ptr_mut(self, len).assume_init() }
2778+
#flex_ref_mut_inner
27622779
}
27632780

27642781
/// Construct DST variant from a pointer and a size.
27652782
///
27662783
/// NOTE: lifetime of returned reference is not tied to any underlying storage.
27672784
/// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements.
27682785
pub unsafe fn flex_ptr<'unbounded>(ptr: *const Self, len: usize) -> &'unbounded #dst_ty_for_impl {
2769-
unsafe { &*::#prefix::ptr::from_raw_parts(ptr as *const (), len) }
2786+
#flex_ptr_inner
27702787
}
27712788

27722789
/// Construct mutable DST variant from a pointer and a
@@ -2780,14 +2797,7 @@ impl CompInfo {
27802797
ptr: *mut Self,
27812798
len: usize,
27822799
) -> ::#prefix::mem::MaybeUninit<&'unbounded mut #dst_ty_for_impl> {
2783-
unsafe {
2784-
// Initialize reference without ever exposing it, as its possibly uninitialized
2785-
let mut uninit = ::#prefix::mem::MaybeUninit::<&mut #dst_ty_for_impl>::uninit();
2786-
(uninit.as_mut_ptr() as *mut *mut #dst_ty_for_impl)
2787-
.write(::#prefix::ptr::from_raw_parts_mut(ptr as *mut (), len));
2788-
2789-
uninit
2790-
}
2800+
#flex_ptr_mut_inner
27912801
}
27922802
},
27932803
)

0 commit comments

Comments
 (0)