-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support loading trust root CAs on Linux #136
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b816427
Support loading trust roots on Linux
dnadoba 70efc81
Fix linux
dnadoba 2cf3ece
run `swift-format`
dnadoba e035a6b
Apply review feedback
dnadoba 54e4645
Merge branch 'main' into dn-system-trust-roots-linux
dnadoba c8bebfc
Implement `_TinyArray2`
dnadoba 887ca46
fix tests
dnadoba 2e85086
Update benchmarks for _TinyArray2
dnadoba 947d934
run swift-format
dnadoba 199cfb9
use two properties instead of TinyArray2
dnadoba 33fcf5d
remove usage of `_TinyArray2` and rename `insert` to `append`
dnadoba a23f07f
remove TinyArray2 and rename TinyArray1 back to just TinyArray
dnadoba 1eb1ec0
run swift-format
dnadoba 487853b
fix tests
dnadoba ed47ec0
Fix review comments
dnadoba 9a5f444
Merge branch 'main' into dn-system-trust-roots-linux
dnadoba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftCertificates open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
final class LockedValueBox<Value> { | ||
private let _lock: NSLock = .init() | ||
private var _value: Value | ||
|
||
var unsafeUnlockedValue: Value { | ||
get { _value } | ||
set { _value = newValue } | ||
} | ||
|
||
func lock() { | ||
_lock.lock() | ||
} | ||
|
||
func unlock() { | ||
_lock.unlock() | ||
} | ||
|
||
init(_ value: Value) { | ||
self._value = value | ||
} | ||
|
||
func withLockedValue<Result>( | ||
_ body: (inout Value) throws -> Result | ||
) rethrows -> Result { | ||
try _lock.withLock { | ||
try body(&_value) | ||
} | ||
} | ||
} | ||
|
||
extension LockedValueBox: @unchecked Sendable where Value: Sendable {} | ||
|
||
extension NSLock { | ||
// this API doesn't exist on Linux and therefore we have a copy of it here | ||
func withLock<Result>(_ body: () throws -> Result) rethrows -> Result { | ||
self.lock() | ||
defer { self.unlock() } | ||
return try body() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftCertificates open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// MARK: - Promise | ||
final class Promise<Value, Failure: Error> { | ||
private enum State { | ||
case unfulfilled(observers: [CheckedContinuation<Result<Value, Failure>, Never>]) | ||
case fulfilled(Result<Value, Failure>) | ||
} | ||
|
||
private let state = LockedValueBox(State.unfulfilled(observers: [])) | ||
|
||
init() {} | ||
|
||
fileprivate var result: Result<Value, Failure> { | ||
get async { | ||
self.state.lock() | ||
|
||
switch self.state.unsafeUnlockedValue { | ||
case .fulfilled(let result): | ||
defer { self.state.unlock() } | ||
return result | ||
|
||
case .unfulfilled(var observers): | ||
return await withCheckedContinuation { | ||
(continuation: CheckedContinuation<Result<Value, Failure>, Never>) in | ||
observers.append(continuation) | ||
self.state.unsafeUnlockedValue = .unfulfilled(observers: observers) | ||
self.state.unlock() | ||
} | ||
} | ||
} | ||
} | ||
|
||
func fulfil(with result: Result<Value, Failure>) { | ||
self.state.withLockedValue { state in | ||
switch state { | ||
case .fulfilled(let oldResult): | ||
fatalError("tried to fulfil Promise that is already fulfilled to \(oldResult). New result: \(result)") | ||
case .unfulfilled(let observers): | ||
for observer in observers { | ||
observer.resume(returning: result) | ||
} | ||
state = .fulfilled(result) | ||
} | ||
} | ||
} | ||
|
||
deinit { | ||
switch self.state.unsafeUnlockedValue { | ||
case .fulfilled: | ||
break | ||
case .unfulfilled: | ||
fatalError("unfulfilled Promise leaked") | ||
} | ||
} | ||
} | ||
|
||
extension Promise: Sendable where Value: Sendable {} | ||
|
||
extension Promise { | ||
func succeed(with value: Value) { | ||
self.fulfil(with: .success(value)) | ||
} | ||
|
||
func fail(with error: Failure) { | ||
self.fulfil(with: .failure(error)) | ||
} | ||
} | ||
|
||
// MARK: - Future | ||
|
||
struct Future<Value, Failure: Error> { | ||
private let promise: Promise<Value, Failure> | ||
|
||
init(_ promise: Promise<Value, Failure>) { | ||
self.promise = promise | ||
} | ||
|
||
var result: Result<Value, Failure> { | ||
get async { | ||
await promise.result | ||
} | ||
} | ||
} | ||
|
||
extension Future: Sendable where Value: Sendable {} | ||
|
||
extension Future { | ||
var value: Value { | ||
get async throws { | ||
try await result.get() | ||
} | ||
} | ||
} | ||
|
||
extension Future where Failure == Never { | ||
var value: Value { | ||
get async { | ||
await result.get() | ||
} | ||
} | ||
} | ||
|
||
extension Result where Failure == Never { | ||
func get() -> Success { | ||
switch self { | ||
case .success(let success): | ||
return success | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this optimization is appropriate: we can just take the lock, it'll be uncontended.