Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does swift have a "C Type" protocol?

Tags:

swift

I would like to constrain a generic to a type that can be represented in C. Is there a protocol or type in the Swift standard library that all C types conform to?

func doWithCType<T:CRepresentable>(cValue: T) {
    // do thing with C type
}

If not a CRepresentable, maybe a CStruct type?

Clues:

Using this C type:

typedef struct {
    int hodor;
} Hodor;

I force casted a struct to an incompatible type (causing a crash). This was the error message: Could not cast value of type '__C.Hodor' (0x1035c0700) to 'Swift.CVarArg' (0x107196240).

I can't find "__C" anywhere, but I'm hopeful that there's a distinction for the C types.

like image 330
dave234 Avatar asked Dec 09 '17 05:12

dave234


1 Answers

Is Swift.CVarArg what you need? It is for types that can be passed through C's va_list mechanism for varargs.

There is also CVaListPointer, which is equivalent to va_list *. That appears in the arguments to String(format:).

https://developer.apple.com/documentation/swift/cvararg https://developer.apple.com/documentation/swift/cvalistpointer

like image 50
Ewan Mellor Avatar answered Oct 15 '22 14:10

Ewan Mellor