Skip to content

Adopt ~Copyable in Subprocess #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
428 changes: 359 additions & 69 deletions Sources/Subprocess/API.swift

Large diffs are not rendered by default.

28 changes: 20 additions & 8 deletions Sources/Subprocess/AsyncBufferSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,57 @@
@preconcurrency import SystemPackage
#endif

internal import Dispatch

#if SubprocessSpan
@available(SubprocessSpan, *)
#endif
public struct AsyncBufferSequence: AsyncSequence, Sendable {
public typealias Failure = any Swift.Error
public typealias Element = SequenceOutput.Buffer
public typealias Element = Buffer

#if os(Windows)
internal typealias DiskIO = FileDescriptor
#else
internal typealias DiskIO = DispatchIO
#endif

@_nonSendable
public struct Iterator: AsyncIteratorProtocol {
public typealias Element = SequenceOutput.Buffer
public typealias Element = Buffer

private let diskIO: TrackedPlatformDiskIO
private let diskIO: DiskIO
private var buffer: [UInt8]
private var currentPosition: Int
private var finished: Bool

internal init(diskIO: TrackedPlatformDiskIO) {
internal init(diskIO: DiskIO) {
self.diskIO = diskIO
self.buffer = []
self.currentPosition = 0
self.finished = false
}

public func next() async throws -> SequenceOutput.Buffer? {
public func next() async throws -> Buffer? {
let data = try await self.diskIO.readChunk(
upToLength: readBufferSize
)
if data == nil {
// We finished reading. Close the file descriptor now
try self.diskIO.safelyClose()
#if os(Windows)
try self.diskIO.close()
#else
self.diskIO.close()
#endif
return nil
}
return data
}
}

private let diskIO: TrackedPlatformDiskIO
private let diskIO: DiskIO

internal init(diskIO: TrackedPlatformDiskIO) {
internal init(diskIO: DiskIO) {
self.diskIO = diskIO
}

Expand Down
241 changes: 0 additions & 241 deletions Sources/Subprocess/Atomic.swift

This file was deleted.

10 changes: 5 additions & 5 deletions Sources/Subprocess/Buffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#if SubprocessSpan
@available(SubprocessSpan, *)
#endif
extension SequenceOutput {
extension AsyncBufferSequence {
/// A immutable collection of bytes
public struct Buffer: Sendable {
#if os(Windows)
Expand All @@ -37,7 +37,7 @@ extension SequenceOutput {
#if SubprocessSpan
@available(SubprocessSpan, *)
#endif
extension SequenceOutput.Buffer {
extension AsyncBufferSequence.Buffer {
/// Number of bytes stored in the buffer
public var count: Int {
return self.data.count
Expand All @@ -53,7 +53,7 @@ extension SequenceOutput.Buffer {
#if SubprocessSpan
@available(SubprocessSpan, *)
#endif
extension SequenceOutput.Buffer {
extension AsyncBufferSequence.Buffer {
#if !SubprocessSpan
/// Access the raw bytes stored in this buffer
/// - Parameter body: A closure with an `UnsafeRawBufferPointer` parameter that
Expand Down Expand Up @@ -140,11 +140,11 @@ extension SequenceOutput.Buffer {
#if SubprocessSpan
@available(SubprocessSpan, *)
#endif
extension SequenceOutput.Buffer: Equatable, Hashable {
extension AsyncBufferSequence.Buffer: Equatable, Hashable {
#if os(Windows)
// Compiler generated conformances
#else
public static func == (lhs: SequenceOutput.Buffer, rhs: SequenceOutput.Buffer) -> Bool {
public static func == (lhs: AsyncBufferSequence.Buffer, rhs: AsyncBufferSequence.Buffer) -> Bool {
return lhs.data.elementsEqual(rhs.data)
}

Expand Down
Loading