Skip to content

Commit 2c9a16e

Browse files
committed
Introduce FileDescriptorInput.standardInput, FileDescriptorOutput.standardOutput, and FileDescriptorOutput.standardError to easily bind current process' descriptors to child processes
1 parent e20e387 commit 2c9a16e

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

Sources/Subprocess/Configuration.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public struct Configuration: Sendable {
103103
var _spawnResult = spawnResultBox!.take()!
104104

105105
let execution = _spawnResult.execution
106+
defer {
107+
// Close process file descriptor now we finished monitoring
108+
execution.processIdentifier.close()
109+
}
106110

107111
return try await withAsyncTaskCleanupHandler {
108112
let inputIO = _spawnResult.inputWriteEnd()
@@ -127,9 +131,6 @@ public struct Configuration: Sendable {
127131
for: execution.processIdentifier
128132
)
129133

130-
// Close process file descriptor now we finished monitoring
131-
execution.processIdentifier.close()
132-
133134
return ExecutionResult(
134135
terminationStatus: terminationStatus,
135136
value: try result.get()

Sources/Subprocess/IO/AsyncIO+Linux.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ final class AsyncIO: Sendable {
288288
)
289289
if rc != 0 {
290290
_registration.withLock { storage in
291-
storage.removeValue(forKey: fileDescriptor.rawValue)
291+
_ = storage.removeValue(forKey: fileDescriptor.rawValue)
292292
}
293293

294294
let capturedError = errno

Sources/Subprocess/IO/Input.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ extension InputProtocol where Self == FileDescriptorInput {
179179
closeAfterSpawningProcess: closeAfterSpawningProcess
180180
)
181181
}
182+
183+
/// Create a Subprocess input that reads from the standard input of
184+
/// current process.
185+
///
186+
/// The file descriptor isn't closed afterwards.
187+
public static var standardInput: Self {
188+
return Self.fileDescriptor(
189+
.standardInput,
190+
closeAfterSpawningProcess: false
191+
)
192+
}
182193
}
183194

184195
extension InputProtocol {

Sources/Subprocess/IO/Output.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ extension OutputProtocol where Self == FileDescriptorOutput {
231231
) -> Self {
232232
return .init(fileDescriptor: fd, closeAfterSpawningProcess: closeAfterSpawningProcess)
233233
}
234+
235+
/// Create a Subprocess output that writes output to the standard output of
236+
/// current process.
237+
///
238+
/// The file descriptor isn't closed afterwards.
239+
public static var standardOutput: Self {
240+
return Self.fileDescriptor(
241+
.standardOutput,
242+
closeAfterSpawningProcess: false
243+
)
244+
}
245+
246+
/// Create a Subprocess output that write output to the standard error of
247+
/// current process.
248+
///
249+
/// The file descriptor isn't closed afterwards.
250+
public static var standardError: Self {
251+
return Self.fileDescriptor(
252+
.standardError,
253+
closeAfterSpawningProcess: false
254+
)
255+
}
234256
}
235257

236258
extension OutputProtocol where Self == StringOutput<UTF8> {

0 commit comments

Comments
 (0)