Skip to content
Open
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
28 changes: 14 additions & 14 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-issue-reporting", from: "2.1.0"),
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"605.0.0"),
],
targets: [
Expand All @@ -33,7 +32,6 @@ let package = Package(
"CasePaths1",
"CasePaths2",
"CasePathsMacros",
.product(name: "IssueReporting", package: "swift-issue-reporting"),
]
),
.target(
Expand Down
2 changes: 0 additions & 2 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"605.0.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.13.0"),
],
targets: [
.target(
Expand All @@ -33,7 +32,6 @@ let package = Package(
"CasePaths1",
"CasePaths2",
"CasePathsMacros",
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
]
),
.target(
Expand Down
40 changes: 8 additions & 32 deletions Sources/CasePaths/CasePathable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import IssueReporting

/// A type that provides a collection of all of its case paths.
///
/// Use the `@CasePathable` macro to automatically add case paths, and this conformance, to an
Expand Down Expand Up @@ -465,9 +463,6 @@ extension CasePathable {

/// Unwraps and yields a mutable associated value to a closure.
///
/// > Warning: If the enum's case does not match the given case key path, the mutation will not
/// > be applied, and a runtime warning will be logged. To suppress these warnings, limit calls
/// > to `modify` to instances in which you have already checked the enum case. For example:
/// >
/// > ```swift
/// > switch e {
Expand All @@ -484,36 +479,17 @@ extension CasePathable {
///
/// - Parameters:
/// - keyPath: A case key path to an associated value.
/// - yield: A closure given mutable access to that associated value.
/// - fileID: The fileID where the modify occurs.
/// - filePath: The filePath where the modify occurs.
/// - line: The line where the modify occurs.
/// - column: The column where the modify occurs.
public mutating func modify<Path>(
/// - body: A closure given mutable access to that associated value.
/// - Returns: The result returned by `body`.
public mutating func modify<Path, Result, Failure: Error>(
_ keyPath: CaseKeyPath<Self, Path>,
yield: (inout Path.Value) -> Void,
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) {
_ body: (inout Path.Value) throws(Failure) -> Result
) throws(Failure) -> Result? {
let path = Self.allCasePaths[keyPath: keyPath]
guard var value = path.extract(from: self) else {
reportIssue(
"""
Can't modify '\(String(describing: self))' via \
'CaseKeyPath<\(Self.self), \(Path.Value.self)>' \
(aka '\(String(reflecting: keyPath))')
""",
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
}
yield(&value)
guard var value = path.extract(from: self) else { return nil }
let result = try body(&value)
self = path.embed(value)
return result
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
### Extracting, replacing, and modifying values

- ``subscript(case:)``
- ``modify(_:yield:fileID:filePath:line:column:)``
- ``modify(_:_:)``

### Case properties

Expand Down
30 changes: 6 additions & 24 deletions Sources/CasePaths/Optional+CasePathable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import IssueReporting

extension Optional: CasePathable {
public struct AllCasePaths: CasePath, Hashable, Sendable {
public func embed(_ value: Optional) -> Optional { value }
Expand Down Expand Up @@ -80,31 +78,15 @@ extension Optional where Wrapped: CasePathable {
}

@_disfavoredOverload
public mutating func modify<Path>(
public mutating func modify<Path, Result, Failure: Error>(
_ keyPath: CaseKeyPath<Wrapped, Path>,
yield: (inout Path.Value) -> Void,
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) {
_ body: (inout Path.Value) throws(Failure) -> Result
) throws(Failure) -> Result? {
let path = Wrapped.allCasePaths[keyPath: keyPath]
guard case .some(let wrapped) = self, var value = path.extract(from: wrapped)
else {
reportIssue(
"""
Can't modify '\(String(describing: self))' via \
'CaseKeyPath<\(Self.self), \(Path.Value.self)>' \
(aka '\(String(reflecting: keyPath))')
""",
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
return
}
yield(&value)
else { return nil }
let result = try body(&value)
self = .some(path.embed(value))
return result
}
}
21 changes: 10 additions & 11 deletions Tests/CasePathsTests/CasePathsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,19 @@ struct CasePathsTests {

@Test func modify() {
var foo = Foo.bar(.int(21))
foo.modify(\.bar.int) { $0 *= 2 }
let value = foo.modify(\.bar.int) {
$0 *= 2
return $0
}
#expect(foo == .bar(.int(42)))
#expect(value == 42)
}

#if DEBUG && !os(Linux) && !os(Windows) && !os(WASI) && !os(Android)
@Test func `modify failure`() {
guard ProcessInfo.processInfo.environment["CI"] == nil else { return }
var foo = Foo.bar(.int(21))
withKnownIssue {
foo.modify(\.baz.string) { $0.append("!") }
}
#expect(foo == .bar(.int(21)))
}
#endif
@Test func `modify failure`() {
var foo = Foo.bar(.int(21))
#expect(foo.modify(\.baz.string) { $0.append("!") } == nil)
#expect(foo == .bar(.int(21)))
}

@Test func `manual conformance via 'AnyCasePath'`() {
// A hand-written conformance in the documented 1.x style, vending
Expand Down
Loading