-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support flexible array members in low-level API #19
Comments
The question is: how? Structs with flexible array member are "incomplete", they cannot be As far as I can tell, the only thing we can reliable do is to ignore that member. We could somehow provide the struct foo {
int len;
char data[];
}; /* automatically generated by rust-bindgen 0.70.1 */
#[repr(C)]
#[derive(Default)]
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
impl<T> __IncompleteArrayField<T> {
#[inline]
pub const fn new() -> Self {
__IncompleteArrayField(::std::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] {
::std::slice::from_raw_parts(self.as_ptr(), len)
}
#[inline]
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
}
}
impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_str("__IncompleteArrayField")
}
}
#[repr(C)]
#[derive(Debug)]
pub struct foo {
pub len: ::std::os::raw::c_int,
pub data: __IncompleteArrayField<::std::os::raw::c_char>,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
["Size of foo"][::std::mem::size_of::<foo>() - 4usize];
["Alignment of foo"][::std::mem::align_of::<foo>() - 4usize];
["Offset of field: foo::len"][::std::mem::offset_of!(foo, len) - 0usize];
["Offset of field: foo::data"][::std::mem::offset_of!(foo, data) - 4usize];
}; |
Discussed this with the client. We propose something along the following lines: Given struct foo {
int len;
char data[];
}; (which potentially has many more fields than just these two, of course), generate something like data PartialFoo = PartialFoo {
len :: Int
-- plus other fields, if any, but without the flexible array member
}
-- generated instance, as usual
instance Storable PartialFoo where In addition, we should also generate an instance of this class: class SupportsFlexibleArray a where
arrayMemberOffset :: Proxy a -> Int Moreover, the user must provide an instance of this class: class SupportsFlexibleArray a => HasFlexibleArrayLength a where
arrayMemberLength :: a -> Int Finally, we provide the following infrastructure in newtype WithFlexbileArrayMember a = WithFlexbileArrayMember a
-- Single instance, defined once and for all
instance (Storable a, HasFlexibleArrayLength a) => Storable (WithFlexibleArrayMember a) where
... |
instance (Storable a, HasFlexibleArrayLength a) => Storable (WithFlexibleArrayMember a) where cannot be defined. Even we have One way to understand this is that We can have Storable-like class though, with peek and poke like functions. |
Sorry, I should have answered here. Good point regarding |
See https://en.wikipedia.org/wiki/Flexible_array_member .
The text was updated successfully, but these errors were encountered: