Skip to content

Commit 04ed260

Browse files
committed
Cleanup some unused types in Utilities and use a Mutex instead of NSLock
1 parent 0b167e7 commit 04ed260

File tree

4 files changed

+13
-41
lines changed

4 files changed

+13
-41
lines changed

Sources/CoreFoundation/include/ForSwiftFoundationOnly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959

6060
#if TARGET_OS_LINUX
6161
#include <sys/sysmacros.h>
62+
#include <fcntl.h>
63+
#include <sys/stat.h>
6264
#endif
6365

6466
#if TARGET_OS_ANDROID

Tests/Foundation/TestProcess.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,25 +580,23 @@ class TestProcess : XCTestCase {
580580
task.executableURL = url
581581
task.arguments = []
582582
let stdoutPipe = Pipe()
583-
let dataLock = NSLock()
583+
let stdoutData = Mutex(Data())
584584
task.standardOutput = stdoutPipe
585585

586-
// protected by the task.waitUntilExit()
587-
nonisolated(unsafe) var stdoutData = Data()
588586
stdoutPipe.fileHandleForReading.readabilityHandler = { fh in
589-
dataLock.synchronized {
590-
stdoutData.append(fh.availableData)
587+
stdoutData.withLock {
588+
$0.append(fh.availableData)
591589
}
592590
}
593591
try task.run()
594592
task.waitUntilExit()
595593
stdoutPipe.fileHandleForReading.readabilityHandler = nil
596594

597-
try dataLock.synchronized {
595+
try stdoutData.withLock {
598596
if let d = try stdoutPipe.fileHandleForReading.readToEnd() {
599-
stdoutData.append(d)
597+
$0.append(d)
600598
}
601-
XCTAssertEqual(String(data: stdoutData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines), "No files specified.")
599+
XCTAssertEqual(String(data: $0, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines), "No files specified.")
602600
}
603601
}
604602

Tests/Foundation/TestURLSessionFTP.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#if !os(Windows)
1111

12+
import Synchronization
13+
1214
class TestURLSessionFTP : LoopbackFTPServerTest {
1315
let saveString = """
1416
FTP implementation to test FTP
@@ -65,11 +67,10 @@ class FTPDataTask : NSObject, @unchecked Sendable {
6567
var responseReceivedExpectation: XCTestExpectation?
6668
var hasTransferCompleted = false
6769

68-
private var errorLock = NSLock()
69-
private var _error = false
70+
private let _error = Mutex(false)
7071
public var error: Bool {
71-
get { errorLock.synchronized { _error } }
72-
set { errorLock.synchronized { _error = newValue } }
72+
get { _error.withLock { $0 } }
73+
set { _error.withLock { $0 = newValue } }
7374
}
7475

7576
init(with expectation: XCTestExpectation) {

Tests/Foundation/Utilities.swift

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -640,35 +640,6 @@ extension String {
640640
}
641641
}
642642

643-
extension FileHandle: @retroactive TextOutputStream {
644-
public func write(_ string: String) {
645-
write(Data(string.utf8))
646-
}
647-
648-
struct EncodedOutputStream: TextOutputStream {
649-
let fileHandle: FileHandle
650-
let encoding: String.Encoding
651-
652-
init(_ fileHandle: FileHandle, encoding: String.Encoding) {
653-
self.fileHandle = fileHandle
654-
self.encoding = encoding
655-
}
656-
657-
func write(_ string: String) {
658-
fileHandle.write(string.data(using: encoding)!)
659-
}
660-
}
661-
}
662-
663-
extension NSLock {
664-
public func synchronized<T>(_ closure: () throws -> T) rethrows -> T {
665-
self.lock()
666-
defer { self.unlock() }
667-
return try closure()
668-
}
669-
}
670-
671-
672643
// Create a uniquely named temporary directory, pass the URL and path to a closure then remove the directory afterwards.
673644
public func withTemporaryDirectory<R>(functionName: String = #function, block: (URL, String) throws -> R) throws -> R {
674645

0 commit comments

Comments
 (0)