From 96604d6d9c88c1eb483078a594fabdf9a8047e94 Mon Sep 17 00:00:00 2001 From: Jeremy Fitzhardinge Date: Wed, 27 Mar 2024 12:42:27 -0700 Subject: [PATCH] Make all the flexarray helpers inline The conversions between fixed and dynamically sized forms are essentially type-level transforms which should have trivial implementations in terms of generated code, so there's no reason not to make them inline. --- .../tests/expectations/tests/flexarray.rs | 25 +++++++++++++++++++ bindgen/codegen/mod.rs | 5 ++++ 2 files changed, 30 insertions(+) diff --git a/bindgen-tests/tests/expectations/tests/flexarray.rs b/bindgen-tests/tests/expectations/tests/flexarray.rs index 46579f2cf2..41d07311c5 100644 --- a/bindgen-tests/tests/expectations/tests/flexarray.rs +++ b/bindgen-tests/tests/expectations/tests/flexarray.rs @@ -69,12 +69,14 @@ impl flexarray<[::std::os::raw::c_int]> { ::std::alloc::Layout::for_value_raw(p) } } + #[inline] pub fn fixed(&self) -> (&flexarray<[::std::os::raw::c_int; 0]>, usize) { unsafe { let (ptr, len) = (self as *const Self).to_raw_parts(); (&*(ptr as *const flexarray<[::std::os::raw::c_int; 0]>), len) } } + #[inline] pub fn fixed_mut(&mut self) -> (&mut flexarray<[::std::os::raw::c_int; 0]>, usize) { unsafe { let (ptr, len) = (self as *mut Self).to_raw_parts(); @@ -92,6 +94,7 @@ impl flexarray<[::std::os::raw::c_int; 0]> { /// Convert a mutable sized prefix to an unsized structure with the given length. /// /// SAFETY: Underlying storage is initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ref_mut( &mut self, len: usize, @@ -102,6 +105,7 @@ impl flexarray<[::std::os::raw::c_int; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ptr<'unbounded>( ptr: *const Self, len: usize, @@ -115,6 +119,7 @@ impl flexarray<[::std::os::raw::c_int; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements. + #[inline] pub unsafe fn flex_ptr_mut<'unbounded>( ptr: *mut Self, len: usize, @@ -165,12 +170,14 @@ impl flexarray_zero<[::std::os::raw::c_int]> { ::std::alloc::Layout::for_value_raw(p) } } + #[inline] pub fn fixed(&self) -> (&flexarray_zero<[::std::os::raw::c_int; 0]>, usize) { unsafe { let (ptr, len) = (self as *const Self).to_raw_parts(); (&*(ptr as *const flexarray_zero<[::std::os::raw::c_int; 0]>), len) } } + #[inline] pub fn fixed_mut( &mut self, ) -> (&mut flexarray_zero<[::std::os::raw::c_int; 0]>, usize) { @@ -193,6 +200,7 @@ impl flexarray_zero<[::std::os::raw::c_int; 0]> { /// Convert a mutable sized prefix to an unsized structure with the given length. /// /// SAFETY: Underlying storage is initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ref_mut( &mut self, len: usize, @@ -203,6 +211,7 @@ impl flexarray_zero<[::std::os::raw::c_int; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ptr<'unbounded>( ptr: *const Self, len: usize, @@ -216,6 +225,7 @@ impl flexarray_zero<[::std::os::raw::c_int; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements. + #[inline] pub unsafe fn flex_ptr_mut<'unbounded>( ptr: *mut Self, len: usize, @@ -244,12 +254,14 @@ impl flexarray_template { ::std::alloc::Layout::for_value_raw(p) } } + #[inline] pub fn fixed(&self) -> (&flexarray_template, usize) { unsafe { let (ptr, len) = (self as *const Self).to_raw_parts(); (&*(ptr as *const flexarray_template), len) } } + #[inline] pub fn fixed_mut(&mut self) -> (&mut flexarray_template, usize) { unsafe { let (ptr, len) = (self as *mut Self).to_raw_parts(); @@ -267,6 +279,7 @@ impl flexarray_template { /// Convert a mutable sized prefix to an unsized structure with the given length. /// /// SAFETY: Underlying storage is initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ref_mut( &mut self, len: usize, @@ -277,6 +290,7 @@ impl flexarray_template { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ptr<'unbounded>( ptr: *const Self, len: usize, @@ -290,6 +304,7 @@ impl flexarray_template { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements. + #[inline] pub unsafe fn flex_ptr_mut<'unbounded>( ptr: *mut Self, len: usize, @@ -404,6 +419,7 @@ impl flexarray_bogus_zero_fam<[::std::os::raw::c_char]> { ::std::alloc::Layout::for_value_raw(p) } } + #[inline] pub fn fixed( &self, ) -> (&flexarray_bogus_zero_fam<[::std::os::raw::c_char; 0]>, usize) { @@ -415,6 +431,7 @@ impl flexarray_bogus_zero_fam<[::std::os::raw::c_char]> { ) } } + #[inline] pub fn fixed_mut( &mut self, ) -> (&mut flexarray_bogus_zero_fam<[::std::os::raw::c_char; 0]>, usize) { @@ -441,6 +458,7 @@ impl flexarray_bogus_zero_fam<[::std::os::raw::c_char; 0]> { /// Convert a mutable sized prefix to an unsized structure with the given length. /// /// SAFETY: Underlying storage is initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ref_mut( &mut self, len: usize, @@ -451,6 +469,7 @@ impl flexarray_bogus_zero_fam<[::std::os::raw::c_char; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ptr<'unbounded>( ptr: *const Self, len: usize, @@ -464,6 +483,7 @@ impl flexarray_bogus_zero_fam<[::std::os::raw::c_char; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements. + #[inline] pub unsafe fn flex_ptr_mut<'unbounded>( ptr: *mut Self, len: usize, @@ -523,12 +543,14 @@ impl flexarray_align<[::std::os::raw::c_int]> { ::std::alloc::Layout::for_value_raw(p) } } + #[inline] pub fn fixed(&self) -> (&flexarray_align<[::std::os::raw::c_int; 0]>, usize) { unsafe { let (ptr, len) = (self as *const Self).to_raw_parts(); (&*(ptr as *const flexarray_align<[::std::os::raw::c_int; 0]>), len) } } + #[inline] pub fn fixed_mut( &mut self, ) -> (&mut flexarray_align<[::std::os::raw::c_int; 0]>, usize) { @@ -551,6 +573,7 @@ impl flexarray_align<[::std::os::raw::c_int; 0]> { /// Convert a mutable sized prefix to an unsized structure with the given length. /// /// SAFETY: Underlying storage is initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ref_mut( &mut self, len: usize, @@ -561,6 +584,7 @@ impl flexarray_align<[::std::os::raw::c_int; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ptr<'unbounded>( ptr: *const Self, len: usize, @@ -574,6 +598,7 @@ impl flexarray_align<[::std::os::raw::c_int; 0]> { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements. + #[inline] pub unsafe fn flex_ptr_mut<'unbounded>( ptr: *mut Self, len: usize, diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 246dac9330..ce0e47deef 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -2702,6 +2702,7 @@ impl CompInfo { ( quote! { + #[inline] pub fn fixed(&self) -> (& #sized_ty_for_impl, usize) { unsafe { let (ptr, len) = (self as *const Self).to_raw_parts(); @@ -2709,6 +2710,7 @@ impl CompInfo { } } + #[inline] pub fn fixed_mut(&mut self) -> (&mut #sized_ty_for_impl, usize) { unsafe { let (ptr, len) = (self as *mut Self).to_raw_parts(); @@ -2728,6 +2730,7 @@ impl CompInfo { /// Convert a mutable sized prefix to an unsized structure with the given length. /// /// SAFETY: Underlying storage is initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ref_mut(&mut self, len: usize) -> &mut #dst_ty_for_impl { // SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`. #flex_ref_mut_inner @@ -2737,6 +2740,7 @@ impl CompInfo { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements. + #[inline] pub unsafe fn flex_ptr<'unbounded>(ptr: *const Self, len: usize) -> &'unbounded #dst_ty_for_impl { #flex_ptr_inner } @@ -2748,6 +2752,7 @@ impl CompInfo { /// /// NOTE: lifetime of returned reference is not tied to any underlying storage. /// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements. + #[inline] pub unsafe fn flex_ptr_mut<'unbounded>( ptr: *mut Self, len: usize,