Skip to content

Underscore 'internal' methods on Case. #215

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

Merged
merged 6 commits into from
Jan 30, 2025
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
38 changes: 19 additions & 19 deletions Sources/CasePathsCore/CasePathable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,30 @@ extension Case {
let keyPath = keyPath.unsafeSendable()
return Case<AppendedValue>(
embed: {
embed(Value.allCasePaths[keyPath: keyPath].embed($0))
_embed(Value.allCasePaths[keyPath: keyPath].embed($0))
},
extract: {
extract(from: $0).flatMap(Value.allCasePaths[keyPath: keyPath].extract)
_extract(from: $0).flatMap(Value.allCasePaths[keyPath: keyPath].extract)
}
)
}

public func embed(_ value: Value) -> Any {
public func _embed(_ value: Value) -> Any {
self._embed(value)
}

public func extract(from root: Any) -> Value? {
public func _extract(from root: Any) -> Value? {
self._extract(root)
}
}

private protocol _AnyCase {
func _extract(from root: Any) -> Any?
func extractAny(from root: Any) -> Any?
}

extension Case: _AnyCase {
fileprivate func _extract(from root: Any) -> Any? {
self.extract(from: root)
fileprivate func extractAny(from root: Any) -> Any? {
self._extract(from: root)
}
}

Expand Down Expand Up @@ -202,7 +202,7 @@ extension CaseKeyPath {
/// - Returns: An enum for the case of this key path that holds the given value.
public func callAsFunction<Enum, AssociatedValue>(_ value: AssociatedValue) -> Enum
where Root == Case<Enum>, Value == Case<AssociatedValue> {
Case(self).embed(value) as! Enum
Case(self)._embed(value) as! Enum
}

/// Returns an enum for this case key path's case.
Expand All @@ -227,7 +227,7 @@ extension CaseKeyPath {
/// - Returns: An enum for the case of this key path.
public func callAsFunction<Enum>() -> Enum
where Root == Case<Enum>, Value == Case<Void> {
Case(self).embed(()) as! Enum
Case(self)._embed(()) as! Enum
}

/// Whether an argument matches the case key path's case.
Expand Down Expand Up @@ -280,8 +280,8 @@ extension _AppendKeyPath {
) -> Enum?
where Self == PartialCaseKeyPath<Enum> {
func open<AnyAssociatedValue>(_ value: AnyAssociatedValue) -> Enum? {
(Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue>)?.embed(value) as? Enum
?? (Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue?>)?.embed(value) as? Enum
(Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue>)?._embed(value) as? Enum
?? (Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue?>)?._embed(value) as? Enum
}
return _openExistential(value, do: open)
}
Expand Down Expand Up @@ -324,13 +324,13 @@ extension CasePathable {
/// enum, and see ``Swift/KeyPath/callAsFunction(_:)`` for embedding an associated value in a
/// brand new root enum.
public subscript<Value>(case keyPath: CaseKeyPath<Self, Value>) -> Value? {
Case(keyPath).extract(from: self)
Case(keyPath)._extract(from: self)
}

/// Attempts to extract the associated value from a root enum using a partial case key path.
@_disfavoredOverload
public subscript(case keyPath: PartialCaseKeyPath<Self>) -> Any? {
(Case<Self>()[keyPath: keyPath] as? any _AnyCase)?._extract(from: self)
(Case<Self>()[keyPath: keyPath] as? any _AnyCase)?.extractAny(from: self)
}

/// Replaces the associated value of a root enum at a case key path when the case matches.
Expand Down Expand Up @@ -362,8 +362,8 @@ extension CasePathable {
get { fatalError() }
set {
let `case` = Case(keyPath)
guard `case`.extract(from: self) != nil else { return }
self = `case`.embed(newValue) as! Self
guard `case`._extract(from: self) != nil else { return }
self = `case`._embed(newValue) as! Self
}
}

Expand Down Expand Up @@ -475,7 +475,7 @@ extension CasePathable {
column: UInt = #column
) {
let `case` = Case(keyPath)
guard var value = `case`.extract(from: self) else {
guard var value = `case`._extract(from: self) else {
reportIssue(
"""
Can't modify '\(String(describing: self))' via 'CaseKeyPath<\(Self.self), \(Value.self)>' \
Expand All @@ -489,7 +489,7 @@ extension CasePathable {
return
}
yield(&value)
self = `case`.embed(value) as! Self
self = `case`._embed(value) as! Self
}
}

Expand All @@ -500,8 +500,8 @@ extension AnyCasePath {
public init(_ keyPath: CaseKeyPath<Root, Value>) {
let `case` = Case(keyPath)
self.init(
embed: { `case`.embed($0) as! Root },
extract: { `case`.extract(from: $0) }
embed: { `case`._embed($0) as! Root },
extract: { `case`._extract(from: $0) }
)
}
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/CasePathsTests/CompileTimeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CasePaths

@CasePathable
private enum EnumWithExtractAndEmbedCase {
case embed
case extract
}