FFI type aliases for specific variants of a generic Rust type?

Hi all. Apologies if this has been answered elsewhere; a bit of digging didn't turn up anything.

I have a (type)[Marlu/jones.rs at 582016f522ae73caf7c7d2a59ca08c37dc09c1b4 · MWATelescope/Marlu · GitHub] that can either be Jones<f64> or Jones`:

use num_complex::Complex
use num_traits::{float::FloatCore, Float, Num, NumAssign, Zero};

#[repr(transparent)]
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct Jones<F: Float + Num>([Complex<F>; 4]);

This works great for Rust code. But, I use this same type for some CUDA code that is "baked" into my Rust code. To copy to/from the device, I've defined two C++ structs that I cast to/from, and I expose them to Rust with bindgen.

typedef struct JonesF32 {
    // Real part of the (0,0) component
    float j00_re;
    // Imaginary part of the (0,0) component
    float j00_im;
    // Real part of the (0,1) component
    float j01_re;
    // Imaginary part of the (0,1) component
    float j01_im;
    // Real part of the (1,0) component
    float j10_re;
    // Imaginary part of the (1, 0) component
    float j10_im;
    // Real part of the (1,1) component
    float j11_re;
    // Imaginary part of the (1,1) component
    float j11_im;
} JonesF32;

typedef struct JonesF64 {
    // Real part of the (0,0) component
    double j00_re;
    // Imaginary part of the (0,0) component
    double j00_im;
    // Real part of the (0,1) component
    double j01_re;
    // Imaginary part of the (0,1) component
    double j01_im;
    // Real part of the (1,0) component
    double j10_re;
    // Imaginary part of the (1, 0) component
    double j10_im;
    // Real part of the (1,1) component
    double j11_re;
    // Imaginary part of the (1,1) component
    double j11_im;
} JonesF64;

This feels very dirty. Is there a way to keep using my generic Jones type on the Rust side without having to cast to/from these types on the CUDA side? Or to generally handle this kind of thing cleaner? Thanks in advance!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.