Skip to content

Conversation

@mulimoen
Copy link
Collaborator

@mulimoen mulimoen commented Apr 8, 2022

Closes #208


unsafe impl<T: H5Type> H5Type for Complex<T> {
fn type_descriptor() -> TypeDescriptor {
TypeDescriptor::Compound(CompoundType {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inherently unsafe, how do we know that offsets are right? (e.g. should we check actual offsets here?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how we would check the offsets

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require us to make something to point at. We could make an instance of MaybeUninit, but field access is only a future feature

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I guess we could do something like this, does that make sense? https://rust.godbolt.org/z/TWbavj98G

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do

unsafe impl<T: H5Type> H5Type for Complex<T> {
    fn type_descriptor() -> TypeDescriptor {
        use std::mem::MaybeUninit;
        // MaybeUninit has the same size and is transparent, but we can
        // do some pointer arithmetics on a real instance
        let dummystruct: Complex<MaybeUninit<T>> =
            Complex::new(MaybeUninit::uninit(), MaybeUninit::uninit());

        assert_eq!(size_of_val(&dummystruct), size_of::<Complex<T>>());

        let base = addr_of!(dummystruct).cast::<u8>();
        let ptr_r = addr_of!(dummystruct.re).cast::<u8>();
        let ptr_i = addr_of!(dummystruct.im).cast::<u8>();

        TypeDescriptor::Compound(CompoundType {
            fields: vec![
                // Compatible with h5py definition of complex
                CompoundField::typed::<T>("r", unsafe { ptr_r.offset_from(base) } as usize, 0),
                CompoundField::typed::<T>("i", unsafe { ptr_i.offset_from(base) } as usize, 1),
            ],
            size: size_of::<Complex<T>>(),
        })
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add this link in a comment in code? E.g. "Complex<T> should be FFI-equivalent to [T; 2]: ".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added this as a comment under type_descriptor()

@aldanor
Copy link
Owner

aldanor commented Jun 9, 2023

Minor nits aside, needs to be rebased on master (because of the changelog).

@aldanor aldanor merged commit eb75034 into aldanor:master Jun 10, 2023
@aldanor aldanor deleted the feature/complex branch June 10, 2023 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Store num_complex datatypes

2 participants