I am reading “Calling Functions With Pointer Parameters“ and right at the beginning it says that …When calling a function that takes a pointer as a parameter, you can use implicit casting to pass a compatible pointer type or implicit bridging to pass a pointer to a variable or the contents of an array.
By pointer, the above means one of the Unsafe*Pointer types, I believe.
The text then proceeds to explain how various magical things happen, e.g., how a function expecting an Unsafe*Pointer can be called with …a[Type] value, which is passed as a pointer to the start of the array. So for example, the following code totally works:
MTLDevice device = ...
var a = [Int](repeating: 10, count 100)
device.makeBuffer(bytes: a, // or &a works too
...)
where makeBuffer(bytes:length:options:)'s signature is
func makeBuffer(
bytes pointer: UnsafeRawPointer, // <= the pointer parameter
length: Int,
options: MTLResourceOptions = []
) -> (any MTLBuffer)?
At first I thought Array conforms to something that makes it possible for the the implicit casting to happen. It indeed does conform to ContiguousBytes but it doesn’t seem like the conformance has anything to do it.
What are implicit casting and implicit bridging? The language reference does not seem to have anything related so how does this work?
The original Calling… article referenced at the top also has a note that Only Swift function types with C function reference calling convention may be used for function pointer arguments. Like a C function pointer, a Swift function type with the @convention(c) attribute does not capture the context of its surrounding scope.
So does that mean that, for example, makeBuffer referenced above is @convention(c) annotated? if yes, that does not seem to be visible in the public documentation so how can one tell?
Cheers,