From e35adee88e5e0a8096c36c04b49282f8a261e153 Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Thu, 21 May 2020 21:31:13 +0200
Subject: [PATCH 1/9] Adding tests for AnyView initializer

---
 Tests/OpenSwiftUITests/OpenSwiftUITests.swift | 30 +++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/Tests/OpenSwiftUITests/OpenSwiftUITests.swift b/Tests/OpenSwiftUITests/OpenSwiftUITests.swift
index 0658af0..876b954 100644
--- a/Tests/OpenSwiftUITests/OpenSwiftUITests.swift
+++ b/Tests/OpenSwiftUITests/OpenSwiftUITests.swift
@@ -8,15 +8,35 @@ final class OpenSwiftUITests: XCTestCase {
             Text("World")
         }
         
-        let body = HStack {
-            if true {
-                Text("Hello")
-            }
-            Text("World")
+    }
+
+    func testAnyViewFromValueWithInDoesNotYieldView() {
+        let anyView = AnyView(_fromValue: 42)
+        XCTAssertNil(anyView)
+    }
+
+    func testAnyViewFromValueWithTextYieldsAnyView() {
+        let expectedText = "Hello"
+        let value: Any = Text(expectedText)
+        let anyView = AnyView(_fromValue: value)
+        XCTAssertNotNil(anyView)
+
+        guard let storage = anyView?._storage as? AnyViewStorage<Text> else {
+            XCTFail("View storage is not an AnyViewStorage of Text")
+            return
+        }
+
+        switch storage._view._storage {
+        case .verbatim(let string):
+            XCTAssertEqual(string, expectedText)
+        case .anyTextStorage(let storage):
+            XCTAssertEqual(storage.storage, expectedText)
         }
     }
 
     static var allTests = [
         ("testExample", testExample),
+        ("testAnyViewFromValueWithInDoesNotYieldView", testAnyViewFromValueWithInDoesNotYieldView),
+        ("testAnyViewFromValueWithTextYieldsAnyView", testAnyViewFromValueWithTextYieldsAnyView),
     ]
 }

From 143be54eeafbef34b984e0dad38c5f7d9d9cc809 Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Thu, 21 May 2020 21:31:54 +0200
Subject: [PATCH 2/9] Adding symbol lookup target in C

---
 Package.swift                       |  5 ++++-
 Sources/CSymbols/include/CSymbols.h |  7 +++++++
 Sources/CSymbols/symbols.c          | 13 +++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 Sources/CSymbols/include/CSymbols.h
 create mode 100644 Sources/CSymbols/symbols.c

diff --git a/Package.swift b/Package.swift
index 7ec0d9c..37c77b6 100644
--- a/Package.swift
+++ b/Package.swift
@@ -24,7 +24,10 @@ let package = Package(
         // Targets can depend on other targets in this package, and on products in packages which this package depends on.
         .target(
             name: "OpenSwiftUI",
-            dependencies: ["CoreGraphicsShim"]),
+            dependencies: ["CoreGraphicsShim", "CSymbols"]),
+
+        .target(name: "CSymbols"),
+
         .testTarget(
             name: "OpenSwiftUITests",
             dependencies: ["OpenSwiftUI"]),
diff --git a/Sources/CSymbols/include/CSymbols.h b/Sources/CSymbols/include/CSymbols.h
new file mode 100644
index 0000000..f14df88
--- /dev/null
+++ b/Sources/CSymbols/include/CSymbols.h
@@ -0,0 +1,7 @@
+
+#ifndef csymbols_h
+#define csymbols_h
+
+void *loadAddressForSymbol(const char *symbolName);
+
+#endif
diff --git a/Sources/CSymbols/symbols.c b/Sources/CSymbols/symbols.c
new file mode 100644
index 0000000..7050b32
--- /dev/null
+++ b/Sources/CSymbols/symbols.c
@@ -0,0 +1,13 @@
+
+#ifdef __linux__
+#define _GNU_SOURCE
+#endif
+
+#include <CSymbols.h>
+#include <stddef.h>
+#include <dlfcn.h>
+
+void *loadAddressForSymbol(const char *symbolName) {
+    void *handle = dlopen(NULL, RTLD_GLOBAL);
+    return dlsym(handle, symbolName);
+}

From 56e477a32db2cf3517e7b12f857ee979c6ae0c0b Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Thu, 21 May 2020 21:45:58 +0200
Subject: [PATCH 3/9] Implementing AnyView.init(_fromValue:)

---
 .../Views/AnyView+initFromValue.swift         | 88 +++++++++++++++++++
 Sources/OpenSwiftUI/Views/AnyView.swift       |  4 -
 2 files changed, 88 insertions(+), 4 deletions(-)
 create mode 100644 Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift

diff --git a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
new file mode 100644
index 0000000..86690ea
--- /dev/null
+++ b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
@@ -0,0 +1,88 @@
+import CSymbols
+
+// MARK: AnyView init(_ fromValue: Any) implementation
+
+extension AnyView {
+
+    public init?(_fromValue value: Any) {
+        // Synthesize a fake protocol conformance record to AnyViewConvertible
+        let conformance = ProtocolConformanceRecord(type: type(of: value), witnessTable: 0)
+        let type = unsafeBitCast(conformance, to: AnyViewConvertible.Type.self)
+
+        // Call static function on that type, pretending it conforms to AnyViewConvertible
+        guard let view = type.anyView(from: value) else { return nil }
+        self = view
+    }
+
+}
+
+// MARK: - AnyViewConvertible
+
+// Extract metadata for the `View` Protocol via the mangled name.
+//
+// It needs to be done this way because the associated type
+// prevents us from using `View.Type`.
+private let viewMetadata: ProtocolMetadata = {
+    let module = "OpenSwiftUI"
+    let name = "View"
+    let postfix = "_p"
+    let mangled = "\(module.count)\(module)\(name.count)\(name)\(postfix)"
+    return ProtocolMetadata(type: _typeByName(mangled)!)
+}()
+
+private protocol AnyViewConvertible { }
+extension AnyViewConvertible {
+    static func anyView(from view: Any) -> AnyView? {
+        // Find witness table to View.
+        // This should be equivalent to saying `view as? View`, if it was allowed.
+        guard let witnessTable = _conformsToProtocol(Self.self, viewMetadata.protocolDescriptorVector) else { return nil }
+
+        let pointer = UnsafeMutablePointer<Self>.allocate(capacity: 1)
+        pointer.initialize(to: view as! Self)
+        return anyViewFactory(pointer, Self.self, witnessTable)
+    }
+}
+
+// MARK: - Protocol Runtime Information
+
+private struct ProtocolConformanceRecord {
+    let type: Any.Type
+    let witnessTable: Int
+}
+
+private struct ProtocolDescriptor { }
+
+private struct ProtocolMetadata {
+    let kind: Int
+    let layoutFlags: UInt32
+    let numberOfProtocols: UInt32
+    let protocolDescriptorVector: UnsafeMutablePointer<ProtocolDescriptor>
+
+    init(type: Any.Type) {
+        self = unsafeBitCast(type, to: UnsafeMutablePointer<Self>.self).pointee
+    }
+}
+
+@_silgen_name("swift_conformsToProtocol")
+private func _conformsToProtocol(
+    _ type: Any.Type,
+    _ protocolDescriptor: UnsafeMutablePointer<ProtocolDescriptor>
+) -> UnsafeRawPointer?
+
+// MARK: - Factory method for AnyView
+
+// This has to be public in order to be exported.
+@_silgen_name("_open_swift_ui_anyViewFactory")
+public func _anyViewFactory<C : View>(from view: C) -> AnyView {
+    return AnyView(view)
+}
+
+private typealias AnyViewFactory = @convention(thin) (UnsafeRawPointer, Any.Type, UnsafeRawPointer) ->(AnyView)
+
+// In order to call `_anyViewFactory` without knowing the Type at compile time
+// We can find the address of the function and call it ourselves
+private let anyViewFactory: AnyViewFactory = {
+    let symbolName = "_open_swift_ui_anyViewFactory".cString(using: .utf8)!
+    let pointer = loadAddressForSymbol(symbolName)
+    return unsafeBitCast(pointer, to: AnyViewFactory.self)
+}()
diff --git a/Sources/OpenSwiftUI/Views/AnyView.swift b/Sources/OpenSwiftUI/Views/AnyView.swift
index f6e60ee..7421646 100644
--- a/Sources/OpenSwiftUI/Views/AnyView.swift
+++ b/Sources/OpenSwiftUI/Views/AnyView.swift
@@ -17,10 +17,6 @@ public struct AnyView: View {
         _storage = AnyViewStorage<V>(view)
     }
     
-    public init?(_fromValue value: Any) {
-        fatalError()
-    }
-    
     public typealias Body = Never
     public var body: Never {
         fatalError()

From d998d4e5c96df72630f77e94f21129418a819a76 Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Thu, 21 May 2020 21:46:42 +0200
Subject: [PATCH 4/9] Marking any view as done

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index f3fb993..a2c9faa 100644
--- a/README.md
+++ b/README.md
@@ -159,7 +159,7 @@ Xcode 11.2 or higher is required.
 
 | Status | Name | Notes |
 | --- | --- | --- |
-| ⚠️ | `struct AnyView` | `init?(_fromValue value: Any)` missing. |
+| ✅ | `struct AnyView` | |
 | ✅ | `struct TupleView` | |
 
 ### Drawing and Animation

From c76b69e1f31f2c921afd65cb72197393f7198ace Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Thu, 21 May 2020 23:04:56 +0200
Subject: [PATCH 5/9] Deallocate pointer when returning anyView

---
 Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
index 86690ea..2c76c2b 100644
--- a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
+++ b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
@@ -39,6 +39,7 @@ extension AnyViewConvertible {
 
         let pointer = UnsafeMutablePointer<Self>.allocate(capacity: 1)
         pointer.initialize(to: view as! Self)
+        defer { pointer.deallocate() }
         return anyViewFactory(pointer, Self.self, witnessTable)
     }
 }

From 4c96b8d5f31f2cc147e89a5bbc5f091d11db0e1c Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Thu, 21 May 2020 23:15:35 +0200
Subject: [PATCH 6/9] Using conformance record for clarity

---
 Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
index 2c76c2b..7316dbe 100644
--- a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
+++ b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
@@ -36,11 +36,13 @@ extension AnyViewConvertible {
         // Find witness table to View.
         // This should be equivalent to saying `view as? View`, if it was allowed.
         guard let witnessTable = _conformsToProtocol(Self.self, viewMetadata.protocolDescriptorVector) else { return nil }
+        let conformanceRecord = ProtocolConformanceRecord(type: Self.self,
+                                                          witnessTable: Int(bitPattern: witnessTable))
 
         let pointer = UnsafeMutablePointer<Self>.allocate(capacity: 1)
         pointer.initialize(to: view as! Self)
         defer { pointer.deallocate() }
-        return anyViewFactory(pointer, Self.self, witnessTable)
+        return anyViewFactory(pointer, conformanceRecord)
     }
 }
 
@@ -78,7 +80,7 @@ public func _anyViewFactory<C : View>(from view: C) -> AnyView {
     return AnyView(view)
 }
 
-private typealias AnyViewFactory = @convention(thin) (UnsafeRawPointer, Any.Type, UnsafeRawPointer) ->(AnyView)
+private typealias AnyViewFactory = @convention(thin) (UnsafeRawPointer, ProtocolConformanceRecord) ->(AnyView)
 
 // In order to call `_anyViewFactory` without knowing the Type at compile time
 // We can find the address of the function and call it ourselves

From a5fea6b8687ff1b2fd7f011c01cc29144725f6de Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Sat, 23 May 2020 16:43:53 +0200
Subject: [PATCH 7/9] Using withUnsafePointer(to:) instead of allocating the
 pointer ourselves

---
 Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
index 7316dbe..b4f7707 100644
--- a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
+++ b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
@@ -39,10 +39,9 @@ extension AnyViewConvertible {
         let conformanceRecord = ProtocolConformanceRecord(type: Self.self,
                                                           witnessTable: Int(bitPattern: witnessTable))
 
-        let pointer = UnsafeMutablePointer<Self>.allocate(capacity: 1)
-        pointer.initialize(to: view as! Self)
-        defer { pointer.deallocate() }
-        return anyViewFactory(pointer, conformanceRecord)
+        return withUnsafePointer(to: view as! Self) { pointer in
+            return anyViewFactory(pointer, conformanceRecord)
+        }
     }
 }
 

From e3750c59798b868d2a8cc17e5e45030ce35733c4 Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Thu, 2 Jul 2020 00:29:54 +0200
Subject: [PATCH 8/9] =?UTF-8?q?No=20longer=20using=20dlfcn.h=20to=20load?=
 =?UTF-8?q?=20the=20symbol=20of=20the=20factory=20method=20but=20using=20C?=
 =?UTF-8?q?=E2=80=99s=20extern=20function?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Sources/CSymbols/include/CSymbols.h                 |  2 +-
 Sources/CSymbols/symbols.c                          | 13 ++++---------
 .../OpenSwiftUI/Views/AnyView+initFromValue.swift   |  4 +---
 3 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/Sources/CSymbols/include/CSymbols.h b/Sources/CSymbols/include/CSymbols.h
index f14df88..5888c76 100644
--- a/Sources/CSymbols/include/CSymbols.h
+++ b/Sources/CSymbols/include/CSymbols.h
@@ -2,6 +2,6 @@
 #ifndef csymbols_h
 #define csymbols_h
 
-void *loadAddressForSymbol(const char *symbolName);
+void *anyViewFactorySymbol();
 
 #endif
diff --git a/Sources/CSymbols/symbols.c b/Sources/CSymbols/symbols.c
index 7050b32..2574d47 100644
--- a/Sources/CSymbols/symbols.c
+++ b/Sources/CSymbols/symbols.c
@@ -1,13 +1,8 @@
 
-#ifdef __linux__
-#define _GNU_SOURCE
-#endif
-
 #include <CSymbols.h>
-#include <stddef.h>
-#include <dlfcn.h>
 
-void *loadAddressForSymbol(const char *symbolName) {
-    void *handle = dlopen(NULL, RTLD_GLOBAL);
-    return dlsym(handle, symbolName);
+extern void _open_swift_ui_anyViewFactory(void);
+
+void *anyViewFactorySymbol() {
+    return &_open_swift_ui_anyViewFactory;
 }
diff --git a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
index b4f7707..a519469 100644
--- a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
+++ b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
@@ -84,7 +84,5 @@ private typealias AnyViewFactory = @convention(thin) (UnsafeRawPointer, Protocol
 // In order to call `_anyViewFactory` without knowing the Type at compile time
 // We can find the address of the function and call it ourselves
 private let anyViewFactory: AnyViewFactory = {
-    let symbolName = "_open_swift_ui_anyViewFactory".cString(using: .utf8)!
-    let pointer = loadAddressForSymbol(symbolName)
-    return unsafeBitCast(pointer, to: AnyViewFactory.self)
+    return unsafeBitCast(anyViewFactorySymbol(), to: AnyViewFactory.self)
 }()

From 7fe7eeed5296e7fb0f740b9fa7c8bf34ea31922d Mon Sep 17 00:00:00 2001
From: Mathias Quintero <me@quintero.io>
Date: Tue, 20 Oct 2020 00:08:50 +0200
Subject: [PATCH 9/9] Moving to using associated type requirements kit

---
 Package.resolved                              | 25 ++++++
 Package.swift                                 |  5 +-
 Sources/CSymbols/include/CSymbols.h           |  7 --
 Sources/CSymbols/symbols.c                    |  8 --
 ...iewAssociatedTypeRequirementsVisitor.swift | 20 +++++
 .../Views/AnyView+initFromValue.swift         | 84 ++-----------------
 6 files changed, 54 insertions(+), 95 deletions(-)
 create mode 100644 Package.resolved
 delete mode 100644 Sources/CSymbols/include/CSymbols.h
 delete mode 100644 Sources/CSymbols/symbols.c
 create mode 100644 Sources/OpenSwiftUI/Associated Types/ViewAssociatedTypeRequirementsVisitor.swift

diff --git a/Package.resolved b/Package.resolved
new file mode 100644
index 0000000..7de78ae
--- /dev/null
+++ b/Package.resolved
@@ -0,0 +1,25 @@
+{
+  "object": {
+    "pins": [
+      {
+        "package": "AssociatedTypeRequirementsKit",
+        "repositoryURL": "https://github.com/nerdsupremacist/AssociatedTypeRequirementsKit.git",
+        "state": {
+          "branch": null,
+          "revision": "5c0b95758d2d65fdb98977ee1af5648d9d192a32",
+          "version": "0.2.0"
+        }
+      },
+      {
+        "package": "CoreGraphicsShim",
+        "repositoryURL": "https://github.com/Cosmo/CoreGraphicsShim.git",
+        "state": {
+          "branch": "master",
+          "revision": "d2aec4b41d4f92371198b9a382f35749254f0a80",
+          "version": null
+        }
+      }
+    ]
+  },
+  "version": 1
+}
diff --git a/Package.swift b/Package.swift
index 37c77b6..9285b17 100644
--- a/Package.swift
+++ b/Package.swift
@@ -18,15 +18,14 @@ let package = Package(
         // Dependencies declare other packages that this package depends on.
         // .package(url: /* package url */, from: "1.0.0"),
         .package(url: "https://github.com/Cosmo/CoreGraphicsShim.git", .branch("master")),
+        .package(url: "https://github.com/nerdsupremacist/AssociatedTypeRequirementsKit.git", from: "0.2.0"),
     ],
     targets: [
         // Targets are the basic building blocks of a package. A target can define a module or a test suite.
         // Targets can depend on other targets in this package, and on products in packages which this package depends on.
         .target(
             name: "OpenSwiftUI",
-            dependencies: ["CoreGraphicsShim", "CSymbols"]),
-
-        .target(name: "CSymbols"),
+            dependencies: ["CoreGraphicsShim", "AssociatedTypeRequirementsKit"]),
 
         .testTarget(
             name: "OpenSwiftUITests",
diff --git a/Sources/CSymbols/include/CSymbols.h b/Sources/CSymbols/include/CSymbols.h
deleted file mode 100644
index 5888c76..0000000
--- a/Sources/CSymbols/include/CSymbols.h
+++ /dev/null
@@ -1,7 +0,0 @@
-
-#ifndef csymbols_h
-#define csymbols_h
-
-void *anyViewFactorySymbol();
-
-#endif
diff --git a/Sources/CSymbols/symbols.c b/Sources/CSymbols/symbols.c
deleted file mode 100644
index 2574d47..0000000
--- a/Sources/CSymbols/symbols.c
+++ /dev/null
@@ -1,8 +0,0 @@
-
-#include <CSymbols.h>
-
-extern void _open_swift_ui_anyViewFactory(void);
-
-void *anyViewFactorySymbol() {
-    return &_open_swift_ui_anyViewFactory;
-}
diff --git a/Sources/OpenSwiftUI/Associated Types/ViewAssociatedTypeRequirementsVisitor.swift b/Sources/OpenSwiftUI/Associated Types/ViewAssociatedTypeRequirementsVisitor.swift
new file mode 100644
index 0000000..c27cc71
--- /dev/null
+++ b/Sources/OpenSwiftUI/Associated Types/ViewAssociatedTypeRequirementsVisitor.swift	
@@ -0,0 +1,20 @@
+import AssociatedTypeRequirementsVisitor
+
+/**
+  **For internal use only.** Use to be able to call a function on a view where you don't know the concrete type at Compile Time.
+ Implement `callAsFunction` and a version that can take `Any` will be included as an extension.
+
+ Useful for occassions where your intuition is to cast `Any` to `View`, but Swift will stop you due to the associated type requirement.
+
+ ```swift
+ guard let view = view as? View else { return }
+ // Do something
+ ```
+ */
+protocol ViewAssociatedTypeRequirementsVisitor: AssociatedTypeRequirementsVisitor {
+    associatedtype Visitor = ViewAssociatedTypeRequirementsVisitor
+    associatedtype Input = View
+    associatedtype Output
+
+    func callAsFunction<T : View>(_ value: T) -> Output
+}
diff --git a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
index a519469..a3de9cf 100644
--- a/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
+++ b/Sources/OpenSwiftUI/Views/AnyView+initFromValue.swift
@@ -1,88 +1,18 @@
-import CSymbols
-
-// MARK: AnyView init(_ fromValue: Any) implementation
+import Foundation
 
 extension AnyView {
-
     public init?(_fromValue value: Any) {
-        // Synthesize a fake protocol conformance record to AnyViewConvertible
-        let conformance = ProtocolConformanceRecord(type: type(of: value), witnessTable: 0)
-        let type = unsafeBitCast(conformance, to: AnyViewConvertible.Type.self)
-
-        // Call static function on that type, pretending it conforms to AnyViewConvertible
-        guard let view = type.anyView(from: value) else { return nil }
+        guard let view = ViewTypeEraser.shared(value) else { return nil }
         self = view
     }
-
 }
 
-// MARK: - AnyViewConvertible
-
-// Extract metadata for the `View` Protocol via the mangled name.
-//
-// It needs to be done this way because the associated type
-// prevents us from using `View.Type`.
-private let viewMetadata: ProtocolMetadata = {
-    let module = "OpenSwiftUI"
-    let name = "View"
-    let postfix = "_p"
-    let mangled = "\(module.count)\(module)\(name.count)\(name)\(postfix)"
-    return ProtocolMetadata(type: _typeByName(mangled)!)
-}()
-
-private protocol AnyViewConvertible { }
-extension AnyViewConvertible {
-    static func anyView(from view: Any) -> AnyView? {
-        // Find witness table to View.
-        // This should be equivalent to saying `view as? View`, if it was allowed.
-        guard let witnessTable = _conformsToProtocol(Self.self, viewMetadata.protocolDescriptorVector) else { return nil }
-        let conformanceRecord = ProtocolConformanceRecord(type: Self.self,
-                                                          witnessTable: Int(bitPattern: witnessTable))
-
-        return withUnsafePointer(to: view as! Self) { pointer in
-            return anyViewFactory(pointer, conformanceRecord)
-        }
+private struct ViewTypeEraser: ViewAssociatedTypeRequirementsVisitor {
+    func callAsFunction<T: View>(_ value: T) -> AnyView {
+        return AnyView(value)
     }
 }
 
-// MARK: - Protocol Runtime Information
-
-private struct ProtocolConformanceRecord {
-    let type: Any.Type
-    let witnessTable: Int
+extension ViewTypeEraser {
+    static let shared = ViewTypeEraser()
 }
-
-private struct ProtocolDescriptor { }
-
-private struct ProtocolMetadata {
-    let kind: Int
-    let layoutFlags: UInt32
-    let numberOfProtocols: UInt32
-    let protocolDescriptorVector: UnsafeMutablePointer<ProtocolDescriptor>
-
-    init(type: Any.Type) {
-        self = unsafeBitCast(type, to: UnsafeMutablePointer<Self>.self).pointee
-    }
-}
-
-@_silgen_name("swift_conformsToProtocol")
-private func _conformsToProtocol(
-    _ type: Any.Type,
-    _ protocolDescriptor: UnsafeMutablePointer<ProtocolDescriptor>
-) -> UnsafeRawPointer?
-
-// MARK: - Factory method for AnyView
-
-// This has to be public in order to be exported.
-@_silgen_name("_open_swift_ui_anyViewFactory")
-public func _anyViewFactory<C : View>(from view: C) -> AnyView {
-    return AnyView(view)
-}
-
-private typealias AnyViewFactory = @convention(thin) (UnsafeRawPointer, ProtocolConformanceRecord) ->(AnyView)
-
-// In order to call `_anyViewFactory` without knowing the Type at compile time
-// We can find the address of the function and call it ourselves
-private let anyViewFactory: AnyViewFactory = {
-    return unsafeBitCast(anyViewFactorySymbol(), to: AnyViewFactory.self)
-}()