UnsafePointer
can be implicitly cast to UnsafeRawPointers
when passed as parameters:
var x = 42
func print<T>(address p: UnsafeRawPointer, as type: T.Type) {
print(p.load(as: type))
}
withUnsafePointer(to: &x) { (ptr) in
print(address: ptr, as: Int.self) // Prints "42"
}
However, it seems that UnsafeBufferPointer
cannot be implicitly cast to UnsafeRawBufferPointer
:
var x = 42
func printBuffer<T>(address p: UnsafeRawBufferPointer, as type: T.Type) {
print(p.load(as: type))
}
withUnsafePointer(to: &x) { (ptr) in
let buff = UnsafeBufferPointer(start: ptr, count: 1)
printBuffer(address: buff, as: Int.self) // 1
}
In this snippet, the line marked // 1
errors:
cannot convert value of type 'UnsafeBufferPointer' to expected argument type 'UnsafeRawBufferPointer'
Why is this implicit conversion not possible, when the previous one is allowed?