Skip to content

Cannot create ForeignType with TypedValueRet members #164

Description

@crisbour

I am trying to generate the structs for the FFI to call Rust code from Julia. I am using ForeignType to expose return structs to Julia, but I am running in an issue with members only supporting ValueRet. Is it possible to have TypedValueRet.
I am not familiar with the code base and not sure what I should look for to add support for this.

Error:

error[E0308]: mismatched types
   --> src/types.rs:69:57
    |
69  |                     $( n_marked += mark_queue_obj(ptls, data.$field) as usize; )*
    |                                    --------------       ^^^^^^^^^^^ expected `Ref<'_, '_, Value<'_, '_>>`, found `Ref<'_, '_, TypedValue<'_, '_, ...>>`
    |                                    |
    |                                    arguments to this function are incorrect
...
96  | julia_type_equivalent!(PrimaGatingInfo, min_on_time, max_on_time, min_off_time_factor, max_off_time_facto...
    | ----------------------------------------------------------------------------------------------------------- in this macro invocation
    |
    = note: expected struct `jlrs::data::managed::Ref<'_, '_, jlrs::prelude::Value<'_, '_>>`
               found struct `jlrs::data::managed::Ref<'static, 'static, TypedValue<'static, 'static, i32>>`
note: function defined here
   --> /home/cristi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/jlrs-0.21.1/src/memory/gc.rs:180:15
    |
180 | pub unsafe fn mark_queue_obj(ptls: PTls, obj: ValueRef) -> bool {
    |               ^^^^^^^^^^^^^^
    = note: this error originates in the macro `julia_type_equivalent` (in Nightly builds, run with -Z macro-backtrace for more info)

My structs generation:

macro_rules! typeof_member {
    (sepia2::types::UsbDevice, product_model) => { JuliaString };
    (sepia2::types::UsbDevice, serial_number) => { JuliaString };
    (sepia2::types::ModuleInfo, is_primary) => { Bool }; // Convert bool to jlrs::Bool
    // etc
}

macro_rules! julia_type_equivalent {
    ($type:ident, $($field:ident),*) => {
        struct $type {
            // TODO: Fill in all fields from sepia2::types::$sepia2_type
            // apart from bool, which becomes Bool
            $( $field: TypedValueRet<typeof_member!(sepia2::types::$type, $field)>, )*
        }

        unsafe impl Send for $type {}
        unsafe impl Sync for $type {}

        unsafe impl ForeignType for $type {
            fn mark(ptls: PTls, data: &Self) -> usize {
                // Safety: We mark all referenced managed data.
                unsafe {
                    let mut n_marked = 0;
                    $( n_marked += mark_queue_obj(ptls, data.$field) as usize; )*
                    n_marked
                }
            }
        }


        impl From<sepia2::types::$type> for $type {
            fn from(item: sepia2::types::$type) -> $type {
                Self {
                    $( $field:  item.$field, )*
                }
            }
        }

    };
}

julia_type_equivalent!(UsbDevice,  product_model, serial_number);
julia_type_equivalent!(FwrError,  err_code, phase, location, slot, condition);
// etc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions