Skip to content

Commit

Permalink
declare Sendable conformances along with the types
Browse files Browse the repository at this point in the history
- `Sendable` is fundamental, it is preferably not declared as an extension.
  • Loading branch information
glessard committed Aug 16, 2023
1 parent f32faad commit 87e68fc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 57 deletions.
20 changes: 6 additions & 14 deletions Sources/System/FileDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ extension FileDescriptor {
extension FileDescriptor {
/// The desired read and write access for a newly opened file.
@frozen
public struct AccessMode: RawRepresentable, Hashable, Codable {
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
public struct AccessMode: RawRepresentable, Sendable, Hashable, Codable {
/// The raw C access mode.
@_alwaysEmitIntoClient
public var rawValue: CInt
Expand Down Expand Up @@ -87,7 +88,8 @@ extension FileDescriptor {

/// Options that specify behavior for a newly-opened file.
@frozen
public struct OpenOptions: OptionSet, Hashable, Codable {
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
public struct OpenOptions: OptionSet, Sendable, Hashable, Codable {
/// The raw C options.
@_alwaysEmitIntoClient
public var rawValue: CInt
Expand Down Expand Up @@ -311,7 +313,8 @@ extension FileDescriptor {

/// Options for specifying what a file descriptor's offset is relative to.
@frozen
public struct SeekOrigin: RawRepresentable, Hashable, Codable {
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
public struct SeekOrigin: RawRepresentable, Sendable, Hashable, Codable {
/// The raw C value.
@_alwaysEmitIntoClient
public var rawValue: CInt
Expand Down Expand Up @@ -475,18 +478,7 @@ extension FileDescriptor.OpenOptions
public var debugDescription: String { self.description }
}

#if compiler(>=5.5) && canImport(_Concurrency)
// The decision on whether to make FileDescriptor Sendable or not
// is currently being discussed in https://github.com/apple/swift-system/pull/112
//@available(*, unavailable, message: "File descriptors are not completely thread-safe.")
//extension FileDescriptor: Sendable {}

@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
extension FileDescriptor.AccessMode: Sendable {}

@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
extension FileDescriptor.OpenOptions: Sendable {}

@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
extension FileDescriptor.SeekOrigin: Sendable {}
#endif
7 changes: 1 addition & 6 deletions Sources/System/FilePath/FilePath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/// are file-system–specific and have additional considerations
/// like case insensitivity, Unicode normalization, and symbolic links.
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
public struct FilePath {
public struct FilePath: Sendable {
// TODO(docs): Section on all the new syntactic operations, lexical normalization, decomposition,
// components, etc.
internal var _storage: SystemString
Expand Down Expand Up @@ -67,8 +67,3 @@ extension FilePath {

@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
extension FilePath: Hashable, Codable {}

#if compiler(>=5.5) && canImport(_Concurrency)
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
extension FilePath: Sendable {}
#endif
15 changes: 5 additions & 10 deletions Sources/System/FilePath/FilePathComponentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ extension FilePath {
///
/// path.components.removeAll { $0.kind == .currentDirectory }
/// // path is "/home/username/bin/scripts/tree"
public struct ComponentView {
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
public struct ComponentView: Sendable {
internal var _path: FilePath
internal var _start: SystemString.Index

Expand Down Expand Up @@ -66,7 +67,9 @@ extension FilePath {
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
extension FilePath.ComponentView: BidirectionalCollection {
public typealias Element = FilePath.Component
public struct Index: Comparable, Hashable {

@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
public struct Index: Sendable, Comparable, Hashable {
internal typealias Storage = SystemString.Index

internal var _storage: Storage
Expand Down Expand Up @@ -214,11 +217,3 @@ extension FilePath.ComponentView {
#endif // DEBUG
}
}

#if compiler(>=5.5) && canImport(_Concurrency)
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
extension FilePath.ComponentView: Sendable {}

@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
extension FilePath.ComponentView.Index: Sendable {}
#endif
20 changes: 6 additions & 14 deletions Sources/System/FilePath/FilePathComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ extension FilePath {
/// * `\\server\share\`
/// * `\\?\UNC\server\share\`
/// * `\\?\Volume{12345678-abcd-1111-2222-123445789abc}\`
public struct Root {
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
public struct Root: Sendable {
internal var _path: FilePath
internal var _rootEnd: SystemString.Index

Expand All @@ -54,7 +55,8 @@ extension FilePath {
/// file.kind == .regular // true
/// file.extension // "txt"
/// path.append(file) // path is "/tmp/foo.txt"
public struct Component {
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
public struct Component: Sendable {
internal var _path: FilePath
internal var _range: Range<SystemString.Index>

Expand All @@ -78,7 +80,8 @@ extension FilePath.Component {
/// Whether a component is a regular file or directory name, or a special
/// directory `.` or `..`
@frozen
public enum Kind {
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
public enum Kind: Sendable {
/// The special directory `.`, representing the current directory.
case currentDirectory

Expand Down Expand Up @@ -285,14 +288,3 @@ extension FilePath.Root {
#endif
}
}

#if compiler(>=5.5) && canImport(_Concurrency)
@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
extension FilePath.Root: Sendable {}

@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
extension FilePath.Component: Sendable {}

@available(/*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8, *)
extension FilePath.Component.Kind: Sendable {}
#endif
7 changes: 1 addition & 6 deletions Sources/System/FilePermissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// perms == [.ownerReadWrite, .groupRead, .otherRead] // true
@frozen
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
public struct FilePermissions: OptionSet, Hashable, Codable {
public struct FilePermissions: OptionSet, Sendable, Hashable, Codable {
/// The raw C file permissions.
@_alwaysEmitIntoClient
public let rawValue: CModeT
Expand Down Expand Up @@ -175,8 +175,3 @@ extension FilePermissions
/// A textual representation of the file permissions, suitable for debugging.
public var debugDescription: String { self.description }
}

#if compiler(>=5.5) && canImport(_Concurrency)
@available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *)
extension FilePermissions: Sendable {}
#endif
10 changes: 3 additions & 7 deletions Sources/System/SystemString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/

// A platform-native character representation, currently used for file paths
internal struct SystemChar: RawRepresentable, Comparable, Hashable, Codable {
internal struct SystemChar:
RawRepresentable, Sendable, Comparable, Hashable, Codable {
internal typealias RawValue = CInterop.PlatformChar

internal var rawValue: RawValue
Expand Down Expand Up @@ -61,7 +62,7 @@ extension SystemChar {
// A platform-native string representation, currently for file paths
//
// Always null-terminated.
internal struct SystemString {
internal struct SystemString: Sendable {
internal typealias Storage = [SystemChar]
internal var nullTerminatedStorage: Storage
}
Expand Down Expand Up @@ -288,11 +289,6 @@ extension SystemString {
}
}

#if compiler(>=5.5) && canImport(_Concurrency)
extension SystemChar: Sendable {}
extension SystemString: Sendable {}
#endif

// TODO: SystemString should use a COW-interchangable storage form rather
// than array, so you could "borrow" the storage from a non-bridged String
// or Data or whatever

0 comments on commit 87e68fc

Please sign in to comment.