Skip to content

Commit 4eb0054

Browse files
committed
Delete Links
1 parent d514647 commit 4eb0054

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// Continuation+Extensions.swift
3+
// FlyingFox
4+
//
5+
// Created by Simon Whitty on 17/02/2022.
6+
// Copyright © 2022 Simon Whitty. All rights reserved.
7+
//
8+
// Distributed under the permissive MIT license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/FlyingFox
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all
21+
// copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
// SOFTWARE.
30+
//
31+
32+
import Foundation
33+
34+
func withCancellingContinuation<T>(function: String = #function,
35+
returning returnType: T.Type,
36+
body: (CheckedContinuation<T, Error>, inout CancellationHandler) -> Void) async throws -> T {
37+
let inner = CancellationHandler.Inner()
38+
return try await withTaskCancellationHandler(
39+
operation: {
40+
try await withCheckedThrowingContinuation(function: function) { (continuation: CheckedContinuation<T, Error>) in
41+
var handler = CancellationHandler(inner: inner)
42+
body(continuation, &handler)
43+
}
44+
},
45+
onCancel: inner.cancel)
46+
}
47+
48+
struct CancellationHandler {
49+
50+
fileprivate let inner: Inner
51+
52+
@Sendable
53+
mutating func onCancel(_ handler: @escaping () -> Void) {
54+
inner.onCancel(handler)
55+
}
56+
}
57+
58+
extension CancellationHandler {
59+
60+
final class Inner {
61+
62+
private let lock = NSLock()
63+
private var isCancelled: Bool = false
64+
private var handler: (() -> Void)?
65+
66+
@Sendable
67+
func onCancel(_ handler: @escaping () -> Void) {
68+
lock.lock()
69+
self.handler = handler
70+
let isCancelled = self.isCancelled
71+
lock.unlock()
72+
73+
if isCancelled {
74+
handler()
75+
}
76+
}
77+
78+
@Sendable
79+
fileprivate func cancel() {
80+
lock.lock()
81+
isCancelled = true
82+
let handler = self.handler
83+
lock.unlock()
84+
85+
handler?()
86+
}
87+
}
88+
}
89+
90+
91+

FlyingFox/Sources/Locked.swift

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Locked.swift
3+
// FlyingFox
4+
//
5+
// Created by Simon Whitty on 24/03/2022.
6+
// Copyright © 2022 Simon Whitty. All rights reserved.
7+
//
8+
// Distributed under the permissive MIT license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/FlyingFox
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all
21+
// copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
// SOFTWARE.
30+
//
31+
32+
import Foundation
33+
34+
@propertyWrapper
35+
final class Locked<Value> {
36+
private var value: Value
37+
38+
init(wrappedValue initialValue: Value) {
39+
self.value = initialValue
40+
}
41+
42+
@available(*, unavailable, message: "@Locked can only be applied to classes")
43+
var wrappedValue: Value { get { fatalError() } set { fatalError() } }
44+
45+
// Classes get and set `wrappedValue` using this subscript.
46+
static subscript<T>(_enclosingInstance instance: T,
47+
wrapped wrappedKeyPath: ReferenceWritableKeyPath<T, Value>,
48+
storage storageKeyPath: ReferenceWritableKeyPath<T, Locked>) -> Value {
49+
get {
50+
instance[keyPath: storageKeyPath].unlock { $0 }
51+
}
52+
set {
53+
instance[keyPath: storageKeyPath].unlock {
54+
$0 = newValue
55+
}
56+
}
57+
}
58+
59+
@discardableResult
60+
func unlock<U>(_ transform: (inout Value) throws -> U) rethrows -> U {
61+
lock.lock()
62+
defer { lock.unlock() }
63+
return try transform(&value)
64+
}
65+
66+
private let lock = NSLock()
67+
}
68+
69+
extension Locked: @unchecked Sendable where Value: Sendable { }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// SocketAddress+Glibc.swift
3+
// FlyingFox
4+
//
5+
// Created by Simon Whitty on 15/03/2022.
6+
// Copyright © 2022 Simon Whitty. All rights reserved.
7+
//
8+
// Distributed under the permissive MIT license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/FlyingFox
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all
21+
// copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
// SOFTWARE.
30+
//
31+
32+
#if canImport(Glibc) && compiler(<5.7)
33+
// swift 5.6 on linux fails to import comformance for these Glibc types 🤷🏻‍♂️:
34+
import Glibc
35+
import FlyingSocks
36+
37+
extension sockaddr_in: SocketAddress {
38+
public static let family = sa_family_t(AF_INET)
39+
}
40+
41+
extension sockaddr_in6: SocketAddress {
42+
public static let family = sa_family_t(AF_INET6)
43+
}
44+
45+
extension sockaddr_un: SocketAddress {
46+
public static let family = sa_family_t(AF_UNIX)
47+
}
48+
49+
#endif

0 commit comments

Comments
 (0)