Skip to content

Commit 41e8b27

Browse files
authored
Merge pull request #1659 from compnerd/file-pipe
NSFileHandle/NSPipe compatibility
2 parents a2b4095 + 5bcd740 commit 41e8b27

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

Foundation/FileHandle.swift

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ open class FileHandle : NSObject, NSSecureCoding {
1919
internal var _fd: Int32
2020
internal var _closeOnDealloc: Bool
2121
internal var _closed: Bool = false
22-
22+
23+
open var readabilityHandler: ((FileHandle) -> Void)? = {
24+
(FileHandle) -> Void in NSUnimplemented()
25+
}
26+
open var writeabilityHandler: ((FileHandle) -> Void)? = {
27+
(FileHandle) -> Void in NSUnimplemented()
28+
}
29+
2330
open var availableData: Data {
2431
return _readDataOfLength(Int.max, untilEOF: false)
2532
}
@@ -346,14 +353,6 @@ extension FileHandle {
346353
open func waitForDataInBackgroundAndNotify() {
347354
NSUnimplemented()
348355
}
349-
350-
open var readabilityHandler: ((FileHandle) -> Void)? {
351-
NSUnimplemented()
352-
}
353-
354-
open var writeabilityHandler: ((FileHandle) -> Void)? {
355-
NSUnimplemented()
356-
}
357356
}
358357

359358
extension FileHandle {
@@ -367,9 +366,9 @@ extension FileHandle {
367366
}
368367

369368
open class Pipe: NSObject {
370-
private let readHandle: FileHandle
371-
private let writeHandle: FileHandle
372-
369+
open let fileHandleForReading: FileHandle
370+
open let fileHandleForWriting: FileHandle
371+
373372
public override init() {
374373
/// the `pipe` system call creates two `fd` in a malloc'ed area
375374
var fds = UnsafeMutablePointer<Int32>.allocate(capacity: 2)
@@ -383,19 +382,11 @@ open class Pipe: NSObject {
383382
/// don't need to add a `deinit` to this class
384383

385384
/// Create the read handle from the first fd in `fds`
386-
self.readHandle = FileHandle(fileDescriptor: fds.pointee, closeOnDealloc: true)
385+
self.fileHandleForReading = FileHandle(fileDescriptor: fds.pointee, closeOnDealloc: true)
387386

388387
/// Advance `fds` by one to create the write handle from the second fd
389-
self.writeHandle = FileHandle(fileDescriptor: fds.successor().pointee, closeOnDealloc: true)
388+
self.fileHandleForWriting = FileHandle(fileDescriptor: fds.successor().pointee, closeOnDealloc: true)
390389

391390
super.init()
392391
}
393-
394-
open var fileHandleForReading: FileHandle {
395-
return self.readHandle
396-
}
397-
398-
open var fileHandleForWriting: FileHandle {
399-
return self.writeHandle
400-
}
401392
}

0 commit comments

Comments
 (0)