From 799060071ae65cce9ad7204d2d8f8e8dc83f15a3 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 12 Jul 2018 12:21:19 +0100 Subject: [PATCH] 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 --- Sources/NIO/System.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sources/NIO/System.swift b/Sources/NIO/System.swift index 828c20ea7..7f020547d 100644 --- a/Sources/NIO/System.swift +++ b/Sources/NIO/System.swift @@ -118,10 +118,9 @@ internal func wrapSyscall(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?) throws -> UnsafePointer? { +internal func wrapErrorIsNullReturnCall(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, addressDescriptionLength: socklen_t) throws -> UnsafePointer? { + public static func inet_ntop(addressFamily: CInt, addressBytes: UnsafeRawPointer, addressDescription: UnsafeMutablePointer, addressDescriptionLength: socklen_t) throws -> UnsafePointer { return try wrapErrorIsNullReturnCall { sysInet_ntop(addressFamily, addressBytes, addressDescription, addressDescriptionLength) }