From 657aa5ad261aa1d069d8ace43c04603c51a59e78 Mon Sep 17 00:00:00 2001 From: Joannis Orlandos Date: Wed, 8 May 2024 19:57:05 +0200 Subject: [PATCH] Allow specialization of the RouterResponder --- Sources/Hummingbird/Router/EndpointResponder.swift | 7 ++++++- Sources/Hummingbird/Router/RouterResponder.swift | 6 ++++++ Sources/Hummingbird/Router/Trie/RouterTrie.swift | 6 +++--- Sources/Hummingbird/Router/Trie/Trie+resolve.swift | 4 ++-- Sources/Hummingbird/Router/Trie/Trie+serialize.swift | 3 --- Sources/Hummingbird/Router/TrieRouter.swift | 2 +- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Sources/Hummingbird/Router/EndpointResponder.swift b/Sources/Hummingbird/Router/EndpointResponder.swift index b7495e069..ef84c7d38 100644 --- a/Sources/Hummingbird/Router/EndpointResponder.swift +++ b/Sources/Hummingbird/Router/EndpointResponder.swift @@ -15,13 +15,15 @@ import HTTPTypes /// Stores endpoint responders for each HTTP method +@usableFromInline struct EndpointResponders: Sendable { init(path: String) { self.path = path self.methods = [:] } - public func getResponder(for method: HTTPRequest.Method) -> (any HTTPResponder)? { + @inlinable + public func getResponder(for method: __shared HTTPRequest.Method) -> (any HTTPResponder)? { return self.methods[method] } @@ -41,6 +43,9 @@ struct EndpointResponders: Sendable { } } + @usableFromInline var methods: [HTTPRequest.Method: any HTTPResponder] + + @usableFromInline var path: String } diff --git a/Sources/Hummingbird/Router/RouterResponder.swift b/Sources/Hummingbird/Router/RouterResponder.swift index 83945ce33..07257af4b 100644 --- a/Sources/Hummingbird/Router/RouterResponder.swift +++ b/Sources/Hummingbird/Router/RouterResponder.swift @@ -15,8 +15,13 @@ import NIOCore public struct RouterResponder: HTTPResponder { + @usableFromInline let trie: RouterTrie> + + @usableFromInline let notFoundResponder: any HTTPResponder + + @usableFromInline let options: RouterOptions init( @@ -33,6 +38,7 @@ public struct RouterResponder: HTTPResponder { /// Respond to request by calling correct handler /// - Parameter request: HTTP request /// - Returns: EventLoopFuture that will be fulfilled with the Response + @inlinable public func respond(to request: Request, context: Context) async throws -> Response { let path: String if self.options.contains(.caseInsensitive) { diff --git a/Sources/Hummingbird/Router/Trie/RouterTrie.swift b/Sources/Hummingbird/Router/Trie/RouterTrie.swift index da9304b07..1fc6ee405 100644 --- a/Sources/Hummingbird/Router/Trie/RouterTrie.swift +++ b/Sources/Hummingbird/Router/Trie/RouterTrie.swift @@ -59,15 +59,15 @@ struct Trie: Sendable { init() {} } -@_spi(Internal) public final class RouterTrie: Sendable { +@usableFromInline +internal final class RouterTrie: Sendable { @usableFromInline let trie: Trie @usableFromInline let values: [Value?] - @inlinable - @_spi(Internal) public init(base: RouterPathTrieBuilder) { + internal init(base: RouterPathTrieBuilder) { var trie = Trie() var values: [Value?] = [] diff --git a/Sources/Hummingbird/Router/Trie/Trie+resolve.swift b/Sources/Hummingbird/Router/Trie/Trie+resolve.swift index d3620099a..910af0099 100644 --- a/Sources/Hummingbird/Router/Trie/Trie+resolve.swift +++ b/Sources/Hummingbird/Router/Trie/Trie+resolve.swift @@ -16,8 +16,8 @@ import NIOCore extension RouterTrie { /// Resolve a path to a `Value` if available - @inlinable - @_spi(Internal) public func resolve(_ path: String) -> (value: Value, parameters: Parameters)? { + @usableFromInline + internal func resolve(_ path: String) -> (value: Value, parameters: Parameters)? { let pathComponents = path.split(separator: "/", omittingEmptySubsequences: true) var pathComponentsIterator = pathComponents.makeIterator() var parameters = Parameters() diff --git a/Sources/Hummingbird/Router/Trie/Trie+serialize.swift b/Sources/Hummingbird/Router/Trie/Trie+serialize.swift index b52ba42b8..37afa10f3 100644 --- a/Sources/Hummingbird/Router/Trie/Trie+serialize.swift +++ b/Sources/Hummingbird/Router/Trie/Trie+serialize.swift @@ -15,7 +15,6 @@ import NIOCore extension RouterTrie { - @usableFromInline static func serialize( _ node: RouterPathTrieBuilder.Node, trie: inout Trie, @@ -92,7 +91,6 @@ extension RouterTrie { trie.nodes[nodeIndex].nextSiblingNodeIndex = trie.nodes.count } - @usableFromInline static func serializeChildren( of node: RouterPathTrieBuilder.Node, trie: inout Trie, @@ -105,7 +103,6 @@ extension RouterTrie { } } - @usableFromInline internal static func highestPriorityFirst(lhs: RouterPathTrieBuilder.Node, rhs: RouterPathTrieBuilder.Node) -> Bool { lhs.key.priority > rhs.key.priority } diff --git a/Sources/Hummingbird/Router/TrieRouter.swift b/Sources/Hummingbird/Router/TrieRouter.swift index 2af29f514..9096160b7 100644 --- a/Sources/Hummingbird/Router/TrieRouter.swift +++ b/Sources/Hummingbird/Router/TrieRouter.swift @@ -41,7 +41,7 @@ import HummingbirdCore } } - @_spi(Internal) public func build() -> RouterTrie { + internal func build() -> RouterTrie { .init(base: self) }