1

I want to bind rust library to php. I have .h file with some declarations of functions and structs:

typedef struct {
    const char* content;
    //char content;
    uint32_t len;
} tc_string_t;

typedef struct  {
    tc_string_t result_json;
    tc_string_t error_json;
} tc_response_t;

typedef struct  {
} tc_response_handle_t;

enum tc_response_flags_t {
    tc_response_finished = 1,
};

typedef void (*tc_on_response_t)(
    int32_t request_id,
    tc_string_t result_json,
    tc_string_t error_json,
    int32_t flags);

void tc_json_request_async(
    uint32_t context,
    tc_string_t method,
    tc_string_t params_json,
    int32_t request_id,
    tc_on_response_t on_result);

tc_response_handle_t* tc_json_request(
    uint32_t context,
    tc_string_t method,
    tc_string_t params_json);

tc_response_t tc_read_json_response(const tc_response_handle_t* handle);
void tc_destroy_json_response(const tc_response_handle_t* handle);

When I called from PHP:

$ffi->tc_read_json_response($handle);

I got the error:

FFI return struct/union is not implemented 

How can I implement the return?

miken32
  • 42,008
  • 16
  • 111
  • 154

1 Answers1

0

I believe PHP does not yet support mapping nested structs or unions to FFI\CData. It's essentially not possible to call a C API that will return these types of data.

You can see from the PHP src here: https://github.com/php/php-src/blob/92381864a5b5705b1d772302a673ed51888e2c30/ext/ffi/ffi.c#L343 that the only supported types for properties in a PHP struct essentially scalars or pointers. The error you are seeing is triggered then here: https://github.com/php/php-src/blob/92381864a5b5705b1d772302a673ed51888e2c30/ext/ffi/ffi.c#L2536-L2552

I think the only option would be to write your own C wrapper that provides functions that return simpler data types.

Joe Hoyle
  • 228
  • 1
  • 9