Passing an extern const C struct by pointer possible?

Hi All,

I'm trying to reference a extern const C struct in Swift code to pass to another C function which expects a pointer to the struct, but have been unable to do so.

The definition is:

extern const struct wl_interface wl_compositor_interface;

and the function header is:

static void* wl_registry_bind(
    struct wl_registry *wl_registry,
    uint32_t name,
    const struct wl_interface *interface,
    uint32_t version 
)

My code is:

window._compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1)

but I get a cannot pass immutable value as inout argument: 'wl_compositor_interface' is a 'let' constant error.

I've posted a bug about this (https://bugs.swift.org/browse/SR-9589) explaining the specifics due to a compiler crash I uncovered while trying to figure out how to achieve this, but thought I'd post here to see if there was a way to achieve it curently.

Does it expect the pointer value to be constant, or does it just want to dereference it? If it just wants to dereference it, you can get the pointer by doing:

window._compositor = withUnsafePointer(to: wl_compositor_interface) {
    wl_registry_bind(registry, name, $0, 1)
}

I have tried that but then I get the compiler crash I mentioned in my original post - https://gist.github.com/tokyovigilante/89a1209eb7802b8a035d6f03b63adeae

In that case, until that crash is resolved, you may just need to take a local copy of the struct:

var myCopy = wl_compositor_interface
window._compositor = wl_registry_bind(registry, name, &myCopy, 1)

That gives me the same compiler crash unfortunately. You're right, I may just have to wait till that crash is resolved.

Terms of Service

Privacy Policy

Cookie Policy