Skip to content

Commit

Permalink
Use custom definitions of incomplete array and union fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Jan 23, 2025
1 parent 61e59af commit d2e159c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
99 changes: 99 additions & 0 deletions src/io_uring/bindgen_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! Local versions of types that bindgen would use.
/// This represents an incomplete array field at the end of a struct.
///
/// This is called `__IncompleteArrayField` in bindgen bindings.
#[repr(C)]
#[derive(Default)]
pub struct IncompleteArrayField<T>(::core::marker::PhantomData<T>, [T; 0]);

#[allow(missing_docs)]
impl<T> IncompleteArrayField<T> {
#[inline]
pub const fn new() -> Self {
IncompleteArrayField(::core::marker::PhantomData, [])
}

#[inline]
pub fn as_ptr(&self) -> *const T {
self as *const _ as *const T
}

#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
self as *mut _ as *mut T
}

#[inline]
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
::core::slice::from_raw_parts(self.as_ptr(), len)
}

#[inline]
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
::core::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
}
}

impl<T> ::core::fmt::Debug for IncompleteArrayField<T> {
fn fmt(&self, fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
fmt.write_str("IncompleteArrayField")
}
}

/// This represents a toplevel union field.
///
/// This is called `__BindgenUnionField` in bindgen bindings.
pub struct UnionField<T>(::core::marker::PhantomData<T>);

#[allow(missing_docs)]
impl<T> UnionField<T> {
#[inline]
pub const fn new() -> Self {
UnionField(::core::marker::PhantomData)
}

#[inline]
pub unsafe fn as_ref(&self) -> &T {
::core::mem::transmute(self)
}

#[inline]
pub unsafe fn as_mut(&mut self) -> &mut T {
::core::mem::transmute(self)
}
}

impl<T> ::core::default::Default for UnionField<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}

impl<T> ::core::clone::Clone for UnionField<T> {
#[inline]
fn clone(&self) -> Self {
*self
}
}

impl<T> ::core::marker::Copy for UnionField<T> {}

impl<T> ::core::fmt::Debug for UnionField<T> {
fn fmt(&self, fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
fmt.write_str("UnionField")
}
}

impl<T> ::core::hash::Hash for UnionField<T> {
fn hash<H: ::core::hash::Hasher>(&self, _state: &mut H) {}
}

impl<T> ::core::cmp::PartialEq for UnionField<T> {
fn eq(&self, _other: &UnionField<T>) -> bool {
true
}
}

impl<T> ::core::cmp::Eq for UnionField<T> {}
13 changes: 8 additions & 5 deletions src/io_uring.rs → src/io_uring/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
//! [rustix-uring]: https://crates.io/crates/rustix-uring
#![allow(unsafe_code)]

mod bindgen_types;

use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
use crate::{backend, io};
use bindgen_types::*;
use core::ffi::c_void;
use core::mem::MaybeUninit;
use core::ptr::{null_mut, write_bytes};
Expand Down Expand Up @@ -1210,7 +1213,7 @@ pub struct io_uring_cqe {
pub user_data: io_uring_user_data,
pub res: i32,
pub flags: IoringCqeFlags,
pub big_cqe: sys::__IncompleteArrayField<u64>,
pub big_cqe: IncompleteArrayField<u64>,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -1286,7 +1289,7 @@ pub struct io_uring_probe {
pub ops_len: u8,
pub resv: u16,
pub resv2: [u32; 3],
pub ops: sys::__IncompleteArrayField<io_uring_probe_op>,
pub ops: IncompleteArrayField<io_uring_probe_op>,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -1416,15 +1419,15 @@ pub struct buf_ring_tail_struct {
#[repr(C)]
#[derive(Debug, Default)]
pub struct buf_ring_bufs_struct {
pub bufs: sys::__IncompleteArrayField<io_uring_buf>,
pub bufs: IncompleteArrayField<io_uring_buf>,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct tail_or_bufs_struct {
pub tail: sys::__BindgenUnionField<buf_ring_tail_struct>,
pub bufs: sys::__BindgenUnionField<buf_ring_bufs_struct>,
pub tail: UnionField<buf_ring_tail_struct>,
pub bufs: UnionField<buf_ring_bufs_struct>,
pub union_field: [u64; 2],
}

Expand Down

0 comments on commit d2e159c

Please sign in to comment.