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 authored and emilio committed Apr 1, 2024
1 parent 0478a39 commit 4778df5
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 @@ -2747,13 +2747,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 @@ -2773,6 +2775,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 @@ -2782,6 +2785,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 @@ -2793,6 +2797,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 4778df5

Please sign in to comment.