Skip to content
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

Remove some unnecessary @unchecked Sendables. #233

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

@stephencelis stephencelis Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lose the Sendable? Or is it implicit and can we remove the explicit one above?

Suggested change
private final class _UIBindingEnumToOptionalCase<Base: _UIBinding, Case>: _UIBinding
private final class _UIBindingEnumToOptionalCase<Base: _UIBinding, Case>: _UIBinding, Sendable

Or should it be conditional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_UIBinding is : Sendable so we already get it.

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