Skip to content
Draft
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
14 changes: 13 additions & 1 deletion Sources/System/Errno.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// occurred.
@frozen
@available(System 0.0.1, *)
public struct Errno: RawRepresentable, Error, Hashable, Codable {
public struct Errno: RawRepresentable, Error, Hashable {
/// The raw C error number.
@_alwaysEmitIntoClient
public let rawValue: CInt
Expand Down Expand Up @@ -1562,6 +1562,8 @@ extension Errno: CustomStringConvertible, CustomDebugStringConvertible {
public var debugDescription: String { self.description }
}

#if !$Embedded

@available(System 0.0.1, *)
extension Errno {
@_alwaysEmitIntoClient
Expand All @@ -1571,3 +1573,13 @@ extension Errno {
}
}

@available(System 0.0.1, *)
extension Errno: Codable {}

public typealias SystemError = any Error

#else

public typealias SystemError = Errno

#endif // !$Embedded
24 changes: 20 additions & 4 deletions Sources/System/FileDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/// in the same way as you manage a raw C file handle.
@frozen
@available(System 0.0.1, *)
public struct FileDescriptor: RawRepresentable, Hashable, Codable {
public struct FileDescriptor: RawRepresentable, Hashable {
/// The raw C file handle.
@_alwaysEmitIntoClient
public let rawValue: CInt
Expand Down Expand Up @@ -46,7 +46,7 @@ extension FileDescriptor {
/// The desired read and write access for a newly opened file.
@frozen
@available(System 0.0.1, *)
public struct AccessMode: RawRepresentable, Sendable, Hashable, Codable {
public struct AccessMode: RawRepresentable, Sendable, Hashable {
/// The raw C access mode.
@_alwaysEmitIntoClient
public var rawValue: CInt
Expand Down Expand Up @@ -89,7 +89,7 @@ extension FileDescriptor {
/// Options that specify behavior for a newly-opened file.
@frozen
@available(System 0.0.1, *)
public struct OpenOptions: OptionSet, Sendable, Hashable, Codable {
public struct OpenOptions: OptionSet, Sendable, Hashable {
/// The raw C options.
@_alwaysEmitIntoClient
public var rawValue: CInt
Expand Down Expand Up @@ -327,7 +327,7 @@ extension FileDescriptor {
/// Options for specifying what a file descriptor's offset is relative to.
@frozen
@available(System 0.0.1, *)
public struct SeekOrigin: RawRepresentable, Sendable, Hashable, Codable {
public struct SeekOrigin: RawRepresentable, Sendable, Hashable {
/// The raw C value.
@_alwaysEmitIntoClient
public var rawValue: CInt
Expand Down Expand Up @@ -402,6 +402,22 @@ extension FileDescriptor {
}
}

#if !$Embedded

@available(System 0.0.1, *)
extension FileDescriptor: Codable {}

@available(System 0.0.1, *)
extension FileDescriptor.AccessMode: Codable {}

@available(System 0.0.1, *)
extension FileDescriptor.OpenOptions: Codable {}

@available(System 0.0.1, *)
extension FileDescriptor.SeekOrigin: Codable {}

#endif // !$Embedded

@available(System 0.0.1, *)
extension FileDescriptor.AccessMode
: CustomStringConvertible, CustomDebugStringConvertible
Expand Down
56 changes: 28 additions & 28 deletions Sources/System/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ extension FileDescriptor {
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
#if !os(Windows)
return try path.withCString {
return try path.withCString { (cString) throws(SystemError) in
try FileDescriptor.open(
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
cString, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
}
#else
return try path.withPlatformString {
#else
return try path.withPlatformString { (platformString) throws(SystemError) in
try FileDescriptor.open(
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
platformString, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
}
#endif
}

#if !os(Windows)
// On Darwin, `CInterop.PlatformChar` is less available than
#if !os(Windows)
// On Darwin, `CInterop.PlatformChar` is less available than
// `FileDescriptor.open`, so we need to use `CChar` instead.

/// Opens or creates a file for reading or writing.
///
/// - Parameters:
Expand All @@ -69,7 +69,7 @@ extension FileDescriptor {
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
try FileDescriptor._open(
path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt
).get()
Expand Down Expand Up @@ -116,7 +116,7 @@ extension FileDescriptor {
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
try FileDescriptor._open(
path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt
).get()
Expand Down Expand Up @@ -149,7 +149,7 @@ extension FileDescriptor {
///
/// The corresponding C function is `close`.
@_alwaysEmitIntoClient
public func close() throws { try _close().get() }
public func close() throws(SystemError) { try _close().get() }

@usableFromInline
internal func _close() -> Result<(), Errno> {
Expand All @@ -169,7 +169,7 @@ extension FileDescriptor {
@discardableResult
public func seek(
offset: Int64, from whence: FileDescriptor.SeekOrigin
) throws -> Int64 {
) throws(SystemError) -> Int64 {
try _seek(offset: offset, from: whence).get()
}

Expand All @@ -187,7 +187,7 @@ extension FileDescriptor {
@available(*, unavailable, renamed: "seek")
public func lseek(
offset: Int64, from whence: FileDescriptor.SeekOrigin
) throws -> Int64 {
) throws(SystemError) -> Int64 {
try seek(offset: offset, from: whence)
}

Expand All @@ -214,15 +214,15 @@ extension FileDescriptor {
public func read(
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _read(into: buffer, retryOnInterrupt: retryOnInterrupt).get()
}

@usableFromInline
internal func _read(
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool
) throws -> Result<Int, Errno> {
) throws(SystemError) -> Result<Int, Errno> {
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
system_read(self.rawValue, buffer.baseAddress, buffer.count)
}
Expand Down Expand Up @@ -251,7 +251,7 @@ extension FileDescriptor {
fromAbsoluteOffset offset: Int64,
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _read(
fromAbsoluteOffset: offset,
into: buffer,
Expand All @@ -276,7 +276,7 @@ extension FileDescriptor {
fromAbsoluteOffset offset: Int64,
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try read(
fromAbsoluteOffset: offset,
into: buffer,
Expand All @@ -303,7 +303,7 @@ extension FileDescriptor {
public func write(
_ buffer: UnsafeRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _write(buffer, retryOnInterrupt: retryOnInterrupt).get()
}

Expand Down Expand Up @@ -337,7 +337,7 @@ extension FileDescriptor {
toAbsoluteOffset offset: Int64,
_ buffer: UnsafeRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _write(toAbsoluteOffset: offset, buffer, retryOnInterrupt: retryOnInterrupt).get()
}

Expand All @@ -359,7 +359,7 @@ extension FileDescriptor {
toAbsoluteOffset offset: Int64,
into buffer: UnsafeRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try write(
toAbsoluteOffset: offset,
buffer,
Expand Down Expand Up @@ -402,7 +402,7 @@ extension FileDescriptor {
public func duplicate(
as target: FileDescriptor? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
try _duplicate(as: target, retryOnInterrupt: retryOnInterrupt).get()
}

Expand All @@ -411,7 +411,7 @@ extension FileDescriptor {
internal func _duplicate(
as target: FileDescriptor?,
retryOnInterrupt: Bool
) throws -> Result<FileDescriptor, Errno> {
) throws(SystemError) -> Result<FileDescriptor, Errno> {
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
if let target = target {
return system_dup2(self.rawValue, target.rawValue)
Expand All @@ -422,13 +422,13 @@ extension FileDescriptor {

@_alwaysEmitIntoClient
@available(*, unavailable, renamed: "duplicate")
public func dup() throws -> FileDescriptor {
public func dup() throws(SystemError) -> FileDescriptor {
fatalError("Not implemented")
}

@_alwaysEmitIntoClient
@available(*, unavailable, renamed: "duplicate")
public func dup2() throws -> FileDescriptor {
public func dup2() throws(SystemError) -> FileDescriptor {
fatalError("Not implemented")
}
}
Expand All @@ -444,7 +444,7 @@ extension FileDescriptor {
/// The corresponding C function is `pipe`.
@_alwaysEmitIntoClient
@available(System 1.1.0, *)
public static func pipe() throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
public static func pipe() throws(SystemError) -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
try _pipe().get()
}

Expand Down Expand Up @@ -490,7 +490,7 @@ extension FileDescriptor {
public func resize(
to newSize: Int64,
retryOnInterrupt: Bool = true
) throws {
) throws(SystemError) {
try _resize(
to: newSize,
retryOnInterrupt: retryOnInterrupt
Expand Down Expand Up @@ -537,7 +537,7 @@ extension FilePermissions {
/// afterwards, because of the way reading the creation mask works.
internal static func withCreationMask<R>(
_ permissions: FilePermissions,
body: () throws -> R
body: () throws(SystemError) -> R
) rethrows -> R {
let oldMask = _umask(permissions.rawValue)
defer {
Expand Down
4 changes: 4 additions & 0 deletions Sources/System/FilePath/FilePath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ extension FilePath {
@available(System 0.0.1, *)
extension FilePath: Hashable {}

#if !$Embedded

@available(System 0.0.1, *)
extension FilePath: Codable {
// Encoder is synthesized; it probably should have been explicit and used
Expand All @@ -88,3 +90,5 @@ extension FilePath: Codable {
}
}
}

#endif // !$Embedded
16 changes: 16 additions & 0 deletions Sources/System/FilePath/FilePathComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ extension SystemString {
}
}

#if !$Embedded

// Unifying protocol for common functionality between roots, components,
// and views onto SystemString and FilePath.
internal protocol _StrSlice: _PlatformStringable, Hashable, Codable {
Expand All @@ -138,6 +140,20 @@ internal protocol _StrSlice: _PlatformStringable, Hashable, Codable {

func _invariantCheck()
}

#else

internal protocol _StrSlice: _PlatformStringable, Hashable {
var _storage: SystemString { get }
var _range: Range<SystemString.Index> { get }

init?(_ str: SystemString)

func _invariantCheck()
}

#endif // !$Embedded

extension _StrSlice {
internal var _slice: Slice<SystemString> {
Slice(base: _storage, bounds: _range)
Expand Down
10 changes: 5 additions & 5 deletions Sources/System/FilePath/FilePathString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension FilePath {
}

#if !os(Windows)
// Note: This function should have been opaque, but it shipped as
// Note: This function should have been opaque, but it shipped as
// `@_alwaysEmitIntoClient` in macOS 12/iOS 15, and now it is stuck
// this way forever. (Or until the language provides a way for us
// to declare separate availability for a function's exported symbol
Expand All @@ -84,7 +84,7 @@ extension FilePath {
/// Don't try to store the pointer for later use.
@_alwaysEmitIntoClient
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
) rethrows -> Result {
return try withCString(body)
}
Expand All @@ -102,7 +102,7 @@ extension FilePath {
/// only during the execution of this method.
/// Don't try to store the pointer for later use.
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
) rethrows -> Result {
return try _withPlatformString(body)
}
Expand Down Expand Up @@ -595,8 +595,8 @@ extension FilePath {
/// For backwards compatibility only. This function is equivalent to
/// the preferred `withPlatformString`.
public func withCString<Result>(
_ body: (UnsafePointer<CChar>) throws -> Result
) rethrows -> Result {
_ body: (UnsafePointer<CChar>) throws(SystemError) -> Result
) throws(SystemError) -> Result {
return try _withPlatformString(body)
}
}
Expand Down
Loading
Loading