Cleanup wrapErrorIsNullReturnCall. (#511)

Motivation:

Given that wrapErrorIsNullReturnCall exists purely to wrap syscalls that
return NULL pointers on errors, there is no point allowing the return
value to be nil. Additionally, it's currently weirdly specific about the
return pointer type, so we can make the function generic.

Modifications:

- Make the return value of wrapErrorIsNullReturnCall generic.
- Make the return value of wrapErrorIsNullReturnCall non-optional.

Result:

Easier to use wrapErrorIsNullReturnCall and any matching function
This commit is contained in:
Cory Benfield authored and GitHub committed 2018-07-12 12:21:19 +01:00
1 parent 072babe443
commit 799060071a
1 file changed
+3 -4
+3 -4
View File
@@ -118,10 +118,9 @@ internal func wrapSyscall<T: FixedWidthInteger>(where function: StaticString = #
/* Sorry, we really try hard to not use underscored attributes. In this case however we seem to break the inlining threshold which makes a system call take twice the time, ie. we need this exception. */
@inline(__always)
internal func wrapErrorIsNullReturnCall(where function: StaticString = #function, _ body: () throws -> UnsafePointer<CChar>?) throws -> UnsafePointer<CChar>? {
internal func wrapErrorIsNullReturnCall<T>(where function: StaticString = #function, _ body: () throws -> T?) throws -> T {
while true {
let res = try body()
if res == nil {
guard let res = try body() else {
let err = errno
if err == EINTR {
continue
@@ -368,7 +367,7 @@ internal enum Posix {
@discardableResult
@inline(never)
public static func inet_ntop(addressFamily: CInt, addressBytes: UnsafeRawPointer, addressDescription: UnsafeMutablePointer<CChar>, addressDescriptionLength: socklen_t) throws -> UnsafePointer<CChar>? {
public static func inet_ntop(addressFamily: CInt, addressBytes: UnsafeRawPointer, addressDescription: UnsafeMutablePointer<CChar>, addressDescriptionLength: socklen_t) throws -> UnsafePointer<CChar> {
return try wrapErrorIsNullReturnCall {
sysInet_ntop(addressFamily, addressBytes, addressDescription, addressDescriptionLength)
}