Skip to content

Commit

Permalink
Make all the flexarray helpers inline
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsgf committed Mar 27, 2024
1 parent 17d7b13 commit d0d872c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions bindgen-tests/tests/expectations/tests/flexarray.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2702,13 +2702,15 @@ impl CompInfo {

(
quote! {
#[inline]
pub fn fixed(&self) -> (& #sized_ty_for_impl, usize) {
unsafe {
let (ptr, len) = (self as *const Self).to_raw_parts();
(&*(ptr as *const #sized_ty_for_impl), len)
}
}

#[inline]
pub fn fixed_mut(&mut self) -> (&mut #sized_ty_for_impl, usize) {
unsafe {
let (ptr, len) = (self as *mut Self).to_raw_parts();
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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,
Expand Down

0 comments on commit d0d872c

Please sign in to comment.