Skip to content

Commit

Permalink
Remove some unnecessary @unchecked Sendables. (#233)
Browse files Browse the repository at this point in the history
* Remove some unnecessary @unchecked Sendables.

* Update keypath name.

* wip
  • Loading branch information
mbrandonw authored Sep 26, 2024
1 parent 230de08 commit b39de7d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "f10d2f6e9f8c467a6984b87f3aba67d2119452577271115a38ed7d5e7ada2444",
"originHash" : "21426c7e67e0003e5a1e08c0f4323d81f8190a1dc8c3b2f9dc9f8fed20da343d",
"pins" : [
{
"identity" : "swift-case-paths",
Expand All @@ -24,8 +24,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "bb5059bde9022d69ac516803f4f227d8ac967f71",
"version" : "1.1.0"
"revision" : "6054df64b55186f08b6d0fd87152081b8ad8d613",
"version" : "1.2.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.5.4"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.1.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.2.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.2"),
.package(url: "https://github.com/pointfreeco/swift-perception", from: "1.3.4"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.5.4"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.1.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.2.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.2"),
.package(url: "https://github.com/pointfreeco/swift-perception", from: "1.3.4"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.2.2"),
Expand Down
19 changes: 19 additions & 0 deletions Sources/SwiftNavigation/Internal/KeyPath+Sendable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if compiler(>=6)
public typealias _SendableKeyPath<Root, Value> = any KeyPath<Root, Value> & Sendable
public typealias _SendableWritableKeyPath<Root, Value> = any WritableKeyPath<Root, Value> & Sendable
#else
public typealias _SendableKeyPath<Root, Value> = KeyPath<Root, Value>
public typealias _SendableWritableKeyPath<Root, Value> = WritableKeyPath<Root, Value>
#endif

func sendableKeyPath<Root, Value>(
_ keyPath: KeyPath<Root, Value>
) -> _SendableKeyPath<Root, Value> {
unsafeBitCast(keyPath, to: _SendableKeyPath<Root, Value>.self)
}

func sendableKeyPath<Root, Value>(
_ keyPath: WritableKeyPath<Root, Value>
) -> _SendableWritableKeyPath<Root, Value> {
unsafeBitCast(keyPath, to: _SendableWritableKeyPath<Root, Value>.self)
}
46 changes: 22 additions & 24 deletions Sources/SwiftNavigation/UIBinding.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ConcurrencyExtras
import IssueReporting

/// A property wrapper type that can read and write an observable value.
Expand Down Expand Up @@ -377,7 +378,7 @@ public struct UIBinding<Value>: Sendable {
) -> UIBinding<Member> {
func open(_ location: some _UIBinding<Value>) -> UIBinding<Member> {
UIBinding<Member>(
location: _UIBindingAppendKeyPath(base: location, keyPath: keyPath),
location: _UIBindingAppendKeyPath(base: location, keyPath: sendableKeyPath(keyPath)),
transaction: transaction
)
}
Expand All @@ -395,7 +396,7 @@ public struct UIBinding<Value>: Sendable {
where Value: CasePathable {
func open(_ location: some _UIBinding<Value>) -> UIBinding<Member?> {
UIBinding<Member?>(
location: _UIBindingEnumToOptionalCase(base: location, keyPath: keyPath),
location: _UIBindingEnumToOptionalCase(base: location, keyPath: sendableKeyPath(keyPath)),
transaction: transaction
)
}
Expand All @@ -412,7 +413,7 @@ public struct UIBinding<Value>: Sendable {
where Value == Wrapped? {
func open(_ location: some _UIBinding<Value>) -> UIBinding<Member?> {
UIBinding<Member?>(
location: _UIBindingOptionalToMember(base: location, keyPath: keyPath),
location: _UIBindingOptionalToMember(base: location, keyPath: sendableKeyPath(keyPath)),
transaction: transaction
)
}
Expand All @@ -429,7 +430,7 @@ public struct UIBinding<Value>: Sendable {
where Value == V? {
func open(_ location: some _UIBinding<Value>) -> UIBinding<Member?> {
UIBinding<Member?>(
location: _UIBindingOptionalEnumToCase(base: location, keyPath: keyPath),
location: _UIBindingOptionalEnumToCase(base: location, keyPath: sendableKeyPath(keyPath)),
transaction: transaction
)
}
Expand Down Expand Up @@ -473,14 +474,14 @@ extension UIBinding: Identifiable where Value: Identifiable {
}

/// A unique identifier for a binding.
public struct UIBindingIdentifier: Hashable {
private let location: AnyHashable
public struct UIBindingIdentifier: Hashable, Sendable {
private let location: AnyHashableSendable

/// Creates an instance that uniquely identifies the given binding.
///
/// - Parameter binding: An instance of a binding.
public init<Value>(_ binding: UIBinding<Value>) {
self.location = AnyHashable(binding.location)
self.location = AnyHashableSendable(binding.location)
}
}

Expand Down Expand Up @@ -584,12 +585,10 @@ private final class _UIBindingConstant<Value>: _UIBinding, @unchecked Sendable {
}
}

private final class _UIBindingAppendKeyPath<Base: _UIBinding, Value>: _UIBinding, @unchecked
Sendable
{
private final class _UIBindingAppendKeyPath<Base: _UIBinding, Value>: _UIBinding, Sendable {
let base: Base
let keyPath: WritableKeyPath<Base.Value, Value>
init(base: Base, keyPath: WritableKeyPath<Base.Value, Value>) {
let keyPath: _SendableWritableKeyPath<Base.Value, Value>
init(base: Base, keyPath: _SendableWritableKeyPath<Base.Value, Value>) {
self.base = base
self.keyPath = keyPath
}
Expand Down Expand Up @@ -677,13 +676,12 @@ where Base.Value: Hashable {
}
}

private final class _UIBindingEnumToOptionalCase<Base: _UIBinding, Case>: _UIBinding, @unchecked
Sendable
private final class _UIBindingEnumToOptionalCase<Base: _UIBinding, Case>: _UIBinding
where Base.Value: CasePathable {
let base: Base
let keyPath: KeyPath<Base.Value.AllCasePaths, AnyCasePath<Base.Value, Case>>
let keyPath: _SendableKeyPath<Base.Value.AllCasePaths, AnyCasePath<Base.Value, Case>>
let casePath: AnyCasePath<Base.Value, Case>
init(base: Base, keyPath: KeyPath<Base.Value.AllCasePaths, AnyCasePath<Base.Value, Case>>) {
init(base: Base, keyPath: _SendableKeyPath<Base.Value.AllCasePaths, AnyCasePath<Base.Value, Case>>) {
self.base = base
self.keyPath = keyPath
self.casePath = Base.Value.allCasePaths[keyPath: keyPath]
Expand All @@ -709,7 +707,7 @@ where Base.Value: CasePathable {

private final class _UIBindingOptionalToBool<
Base: _UIBinding<Wrapped?>, Wrapped
>: _UIBinding, @unchecked Sendable {
>: _UIBinding {
let base: Base
let fileID: StaticString
let filePath: StaticString
Expand Down Expand Up @@ -760,10 +758,10 @@ private final class _UIBindingOptionalToBool<

private final class _UIBindingOptionalToMember<
Base: _UIBinding<Wrapped?>, Wrapped, Value
>: _UIBinding, @unchecked Sendable {
>: _UIBinding {
let base: Base
let keyPath: WritableKeyPath<Wrapped, Value>
init(base: Base, keyPath: WritableKeyPath<Wrapped, Value>) {
let keyPath: _SendableWritableKeyPath<Wrapped, Value>
init(base: Base, keyPath: _SendableWritableKeyPath<Wrapped, Value>) {
self.base = base
self.keyPath = keyPath
}
Expand All @@ -790,11 +788,11 @@ private final class _UIBindingOptionalToMember<

private final class _UIBindingOptionalEnumToCase<
Base: _UIBinding<Enum?>, Enum: CasePathable, Case
>: _UIBinding, @unchecked Sendable {
>: _UIBinding {
let base: Base
let keyPath: KeyPath<Enum.AllCasePaths, AnyCasePath<Enum, Case>>
let keyPath: _SendableKeyPath<Enum.AllCasePaths, AnyCasePath<Enum, Case>>
let casePath: AnyCasePath<Enum, Case>
init(base: Base, keyPath: KeyPath<Enum.AllCasePaths, AnyCasePath<Enum, Case>>) {
init(base: Base, keyPath: _SendableKeyPath<Enum.AllCasePaths, AnyCasePath<Enum, Case>>) {
self.base = base
self.keyPath = keyPath
self.casePath = Enum.allCasePaths[keyPath: keyPath]
Expand All @@ -818,7 +816,7 @@ private final class _UIBindingOptionalEnumToCase<
}
}

private final class _UIBindingPrintChanges<Base: _UIBinding>: _UIBinding, @unchecked Sendable {
private final class _UIBindingPrintChanges<Base: _UIBinding>: _UIBinding {
let base: Base
let prefix: String
let fileID: StaticString
Expand Down

0 comments on commit b39de7d

Please sign in to comment.