diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index 1193ec9..28226e4 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -391,10 +391,12 @@ extension FileDescriptor { internal static func _pipe() -> Result<(readEnd: FileDescriptor, writeEnd: FileDescriptor), Errno> { var fds: (Int32, Int32) = (-1, -1) return withUnsafeMutablePointer(to: &fds) { pointer in - valueOrErrno(retryOnInterrupt: false) { - system_pipe(UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: Int32.self)) + pointer.withMemoryRebound(to: Int32.self, capacity: 2) { fds in + valueOrErrno(retryOnInterrupt: false) { + system_pipe(fds) + }.map { _ in (.init(rawValue: fds[0]), .init(rawValue: fds[1])) } } - }.map { _ in (.init(rawValue: fds.0), .init(rawValue: fds.1)) } + } } } #endif diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index be5b6a8..a918a92 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -171,12 +171,15 @@ extension SystemString { internal func withCodeUnits( _ f: (UnsafeBufferPointer) throws -> T ) rethrows -> T { - try withSystemChars { - // TODO: Is this the right way? - let base = UnsafeRawPointer( - $0.baseAddress - )!.assumingMemoryBound(to: CInterop.PlatformUnicodeEncoding.CodeUnit.self) - return try f(UnsafeBufferPointer(start: base, count: $0.count)) + try withSystemChars { chars in + let length = chars.count * MemoryLayout.stride + let count = length / MemoryLayout.stride + return try chars.baseAddress!.withMemoryRebound( + to: CInterop.PlatformUnicodeEncoding.CodeUnit.self, + capacity: count + ) { pointer in + try f(UnsafeBufferPointer(start: pointer, count: count)) + } } } } @@ -272,13 +275,15 @@ extension SystemString { internal func withPlatformString( _ f: (UnsafePointer) throws -> T ) rethrows -> T { - try withSystemChars { - // TODO: Is this the right way? - let base = UnsafeRawPointer( - $0.baseAddress - )!.assumingMemoryBound(to: CInterop.PlatformChar.self) - assert(base[self.count] == 0) - return try f(base) + try withSystemChars { chars in + let length = chars.count * MemoryLayout.stride + return try chars.baseAddress!.withMemoryRebound( + to: CInterop.PlatformChar.self, + capacity: length / MemoryLayout.stride + ) { pointer in + assert(pointer[self.count] == 0) + return try f(pointer) + } } } } diff --git a/Tests/SystemTests/FilePathTests/FilePathTest.swift b/Tests/SystemTests/FilePathTests/FilePathTest.swift index 168eab5..4634129 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTest.swift @@ -22,7 +22,9 @@ func filePathFromUnterminatedBytes(_ bytes: S) -> FilePath where S. array += [0] return array.withUnsafeBufferPointer { - FilePath(platformString: $0.baseAddress!._asCChar) + $0.withMemoryRebound(to: CChar.self) { + FilePath(platformString: $0.baseAddress!) + } } } let invalidBytes: [UInt8] = [0x2F, 0x61, 0x2F, 0x62, 0x2F, 0x83] diff --git a/Tests/SystemTests/SystemStringTests.swift b/Tests/SystemTests/SystemStringTests.swift index bcfe632..bb13aa2 100644 --- a/Tests/SystemTests/SystemStringTests.swift +++ b/Tests/SystemTests/SystemStringTests.swift @@ -10,29 +10,6 @@ // Tests for PlatformString, SystemString, and FilePath's forwarding APIs // TODO: Adapt test to Windows -extension UnsafePointer where Pointee == UInt8 { - internal var _asCChar: UnsafePointer { - UnsafeRawPointer(self).assumingMemoryBound(to: CChar.self) - } -} -extension UnsafePointer where Pointee == CChar { - internal var _asUInt8: UnsafePointer { - UnsafeRawPointer(self).assumingMemoryBound(to: UInt8.self) - } -} -extension UnsafeBufferPointer where Element == UInt8 { - internal var _asCChar: UnsafeBufferPointer { - let base = baseAddress?._asCChar - return UnsafeBufferPointer(start: base, count: self.count) - } -} -extension UnsafeBufferPointer where Element == CChar { - internal var _asUInt8: UnsafeBufferPointer { - let base = baseAddress?._asUInt8 - return UnsafeBufferPointer(start: base, count: self.count) - } -} - import XCTest @@ -46,7 +23,7 @@ private func makeRaw( _ str: String ) -> [CInterop.PlatformChar] { var str = str - var array = str.withUTF8 { Array($0._asCChar) } + var array = str.withUTF8 { $0.withMemoryRebound(to: CChar.self, Array.init) } array.append(0) return array }