I noticed most low-level APIs exposing pointers neither properly handle ~Copyable
types nor propagate the generic error types (typed-throws). Is this on purpose? or Swift compiler devs didn't have the time to "get there" yet?
You can pick almost any pointer API, for example, String.withUTF8(_:)
or ContiguousBytes.withUnsafeBytes(_:)
.
try string.withUTF8 { buffer throws(CustomError) in
guard checkSomething() else {
throw CustomError()
}
return NonCopyableStruct()
}
For the previous case the compiler will tell us that:
- Instance method
withUTF8
requires thatNonCopyableStruct
conforms toCopyable
. - Thrown expression type
any Error
cannot be converted to error typeCustomError
.
I currently have some "ugly code" to overcome these issues. I am curious if someone has a more elegant solution.