"Encouraging" the use of a custom initializer for imported C structs

I'm writing this C struct:

__attribute__((swift_name("Heap")))
typedef struct {
    void *memory;
} MMHeap;

__attribute__((swift_name("Heap.init(memory:block_size:)")))
MMHeap MMHeapInitialize(void *memory, mmheap_ptr_type block_size);

which is imported to Swift as

public struct Heap {

    public var memory: UnsafeMutableRawPointer!

    public init()

    public init(memory: UnsafeMutableRawPointer!)
}
extension Heap {
    public /*not inherited*/ init(memory: UnsafeMutableRawPointer!, block_size: Heap.pointer)
}

Following C convention, Swift emits various initializers for this type. However in reality, not using the provided initializer is undefined. In C, we are generally OK to let the programmer create nonsense structs and have UB at runtime, however in Swift we generally expect Type() to work if it compiles.

I'm looking for a way to turn off the magic, or alternatively to mark the right intializer as designated. I tried

__attribute__((unavailable))
__attribute__((swift_name("Heap.init(memory:)")))
MMHeap ThisFunctionDoesNotExist(void *memory);

on the theory that this might overload the autogenerated one, but this seems not to be the case.

1 Like