Skip to content

Commit

Permalink
Swift 5.7 support: Replace the AssociatedTypeRequirementsKit package …
Browse files Browse the repository at this point in the history
…with native casts (#8)

* Replace the AssociatedTypeRequirementsVisitor package with Swift casts

* remove old code

* update swift-tools-version to 5.7
  • Loading branch information
lukaskollmer authored Sep 27, 2022
1 parent f655813 commit 761c5db
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 65 deletions.
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"object": {
"pins": [
{
"package": "AssociatedTypeRequirementsKit",
"repositoryURL": "https://github.com/nerdsupremacist/AssociatedTypeRequirementsKit.git",
"state": {
"branch": null,
"revision": "2e4c49c21ffb2135f1c99fbfcf2119c9d24f5e8c",
"version": "0.3.2"
}
},
{
"package": "FineJSON",
"repositoryURL": "https://github.com/omochi/FineJSON.git",
Expand Down
6 changes: 2 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

//
// This source file is part of the Apodini open source project
Expand All @@ -21,7 +21,6 @@ let package = Package(
.library(name: "MetadataSystem", targets: ["MetadataSystem"])
],
dependencies: [
.package(url: "https://github.com/nerdsupremacist/AssociatedTypeRequirementsKit.git", .upToNextMinor(from: "0.3.2")),
.package(url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/norio-nomura/XCTAssertCrash.git", from: "0.2.0"),
.package(url: "https://github.com/omochi/FineJSON.git", from: "1.14.0")
Expand All @@ -36,8 +35,7 @@ let package = Package(
.target(
name: "MetadataSystem",
dependencies: [
.target(name: "ApodiniContext"),
.product(name: "AssociatedTypeRequirementsKit", package: "AssociatedTypeRequirementsKit")
.target(name: "ApodiniContext")
]
),
.target(
Expand Down
64 changes: 12 additions & 52 deletions Sources/MetadataSystem/MetadataParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import ApodiniContext
@_implementationOnly import AssociatedTypeRequirementsVisitor

/// An instance of this protocol is used to visit a ``AnyMetadata`` tree.
public protocol MetadataParser {
Expand Down Expand Up @@ -77,14 +76,18 @@ public extension MetadataParser {
extension MetadataParser {
// swiftlint:disable:next identifier_name
func _visit<Definition: MetadataDefinition>(definition: Definition) {
if StandardEmptyMetadataVisitor(parser: self)(definition) == nil {
if let emptyBlock = definition as? any EmptyMetadata {
emptyBlock._accept(parser: self)
} else {
self.visit(definition: definition)
}
}

// swiftlint:disable:next identifier_name
func _visit<Block: AnyMetadataBlock>(block: Block) {
if StandardRestrictedMetadataBlockVisitor(parser: self)(block) == nil {
if let restrictedBlock = block as? any RestrictedMetadataBlock {
restrictedBlock._accept(parser: self)
} else {
self.visit(block: block)
}
}
Expand All @@ -106,58 +109,15 @@ public struct StandardMetadataParser: MetadataParser {
}


private protocol RestrictedMetadataBlockVisitor: AssociatedTypeRequirementsVisitor {
associatedtype Visitor = RestrictedMetadataBlockVisitor
associatedtype Input = RestrictedMetadataBlock
associatedtype Output

func callAsFunction<M: RestrictedMetadataBlock>(_ value: M) -> Output
}

private struct TestBlock: RestrictedMetadataBlock {
typealias RestrictedContent = TestBlock
var typeErasedContent: AnyMetadata {
fatalError("Not implemented!")
}
}

private extension RestrictedMetadataBlockVisitor {
@inline(never)
@_optimize(none)
func _test() {
_ = self(TestBlock())
}
}

private struct StandardRestrictedMetadataBlockVisitor: RestrictedMetadataBlockVisitor {
var parser: MetadataParser
func callAsFunction<M: RestrictedMetadataBlock>(_ value: M) {
parser.visit(restrictedBlock: value)
extension RestrictedMetadataBlock {
fileprivate func _accept(parser: MetadataParser) {
parser.visit(restrictedBlock: self)
}
}


private protocol EmptyMetadataVisitor: AssociatedTypeRequirementsVisitor {
associatedtype Visitor = EmptyMetadataVisitor
associatedtype Input = EmptyMetadata
associatedtype Output

func callAsFunction<M: EmptyMetadata>(_ value: M) -> Output
}

private struct TestEmpty: EmptyMetadata {}

private extension EmptyMetadataVisitor {
@inline(never)
@_optimize(none)
func _test() {
_ = self(TestEmpty())
}
}

private struct StandardEmptyMetadataVisitor: EmptyMetadataVisitor {
var parser: MetadataParser
func callAsFunction<M: EmptyMetadata>(_ value: M) {
parser.visit(empty: value)
extension EmptyMetadata {
fileprivate func _accept(parser: MetadataParser) {
parser.visit(empty: self)
}
}

0 comments on commit 761c5db

Please sign in to comment.