From ec47eb0677db5b6ab707305aedefa2d99e323a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Wei=C3=9F?= Date: Wed, 31 May 2017 19:02:36 +0100 Subject: [PATCH] specialise the syscall wrapper for Int/Int32 --- Sources/Sockets/IO.swift | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/Sources/Sockets/IO.swift b/Sources/Sockets/IO.swift index 6c7a39947..759384b92 100644 --- a/Sources/Sockets/IO.swift +++ b/Sources/Sockets/IO.swift @@ -38,8 +38,8 @@ func reasonForError(errno: Int32, function: String) -> String { } } -func wrapSyscall(function: @autoclosure () -> String, - _ successCondition: (A) -> Bool, _ fn: () -> A) throws -> A { +func wrapSyscall(function: @autoclosure () -> String, + _ successCondition: (Int) -> Bool, _ fn: () -> Int) throws -> Int { do { return try withErrno(hint: "", successCondition: successCondition, fn) } catch let e as POSIXReasonedError { @@ -47,13 +47,13 @@ func wrapSyscall(function: @autoclosure () -> String, } } -func wrapSyscall(_ successCondition: (A) -> Bool, - function: @autoclosure () -> String, _ fn: () -> A) throws -> A { +func wrapSyscall(_ successCondition: (Int) -> Bool, + function: @autoclosure () -> String, _ fn: () -> Int) throws -> Int { return try wrapSyscall(function: function, successCondition, fn) } -func wrapSyscallMayBlock(_ successCondition: (A) -> Bool, - function: @autoclosure () -> String, _ fn: () -> A) throws -> A? { +func wrapSyscallMayBlock(_ successCondition: (Int) -> Bool, + function: @autoclosure () -> String, _ fn: () -> Int) throws -> Int? { do { return try wrapSyscall(function: function, successCondition, fn) } catch let error as IOError { @@ -63,3 +63,29 @@ func wrapSyscallMayBlock(_ successCondition: (A) -> Bool, throw error } } + +func wrapSyscall(function: @autoclosure () -> String, + _ successCondition: (Int32) -> Bool, _ fn: () -> Int32) throws -> Int32 { + do { + return try withErrno(hint: "", successCondition: successCondition, fn) + } catch let e as POSIXReasonedError { + throw ioError(errno: e.code, function: function()) + } +} + +func wrapSyscall(_ successCondition: (Int32) -> Bool, + function: @autoclosure () -> String, _ fn: () -> Int32) throws -> Int32 { + return try wrapSyscall(function: function, successCondition, fn) +} + +func wrapSyscallMayBlock(_ successCondition: (Int32) -> Bool, + function: @autoclosure () -> String, _ fn: () -> Int32) throws -> Int32? { + do { + return try withErrno(hint: "", successCondition: successCondition, fn) + } catch let e as POSIXReasonedError { + if e.code == EWOULDBLOCK { + return nil; + } + throw ioError(errno: e.code, function: function()) + } +}