| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /*
- * Copyright 2022, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #if compiler(>=5.6)
- @usableFromInline
- internal struct ServerInterceptorStateMachine {
- @usableFromInline
- internal private(set) var state: Self.State
- @inlinable
- init() {
- self.state = .intercepting(.init())
- }
- @inlinable
- mutating func interceptRequestMetadata() -> InterceptAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptRequestMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptRequestMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptRequestMessage() -> InterceptAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptRequestMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptRequestMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptRequestEnd() -> InterceptAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptRequestEnd()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptRequestEnd()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptedRequestMetadata() -> InterceptedAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptedRequestMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptedRequestMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptedRequestMessage() -> InterceptedAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptedRequestMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptedRequestMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptedRequestEnd() -> InterceptedAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptedRequestEnd()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptedRequestEnd()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptResponseMetadata() -> InterceptAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptResponseMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptResponseMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptResponseMessage() -> InterceptAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptResponseMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptResponseMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptResponseStatus() -> InterceptAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptResponseStatus()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptResponseStatus()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptedResponseMetadata() -> InterceptedAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptedResponseMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptedResponseMetadata()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptedResponseMessage() -> InterceptedAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptedResponseMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptedResponseMessage()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func interceptedResponseStatus() -> InterceptedAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.interceptedResponseStatus()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.interceptedResponseStatus()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- @inlinable
- mutating func cancel() -> CancelAction {
- switch self.state {
- case var .intercepting(intercepting):
- let nextStateAndOutput = intercepting.cancel()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- case var .finished(finished):
- let nextStateAndOutput = finished.cancel()
- self.state = nextStateAndOutput.nextState.state
- return nextStateAndOutput.output
- }
- }
- }
- extension ServerInterceptorStateMachine {
- /// The possible states the state machine may be in.
- @usableFromInline
- internal enum State {
- case intercepting(ServerInterceptorStateMachine.Intercepting)
- case finished(ServerInterceptorStateMachine.Finished)
- }
- }
- extension ServerInterceptorStateMachine {
- /// The next state to transition to and any output which may be produced as a
- /// result of a substate handling an action.
- @usableFromInline
- internal struct NextStateAndOutput<NextState, Output> {
- @usableFromInline
- internal var nextState: NextState
- @usableFromInline
- internal var output: Output
- @inlinable
- internal init(nextState: NextState, output: Output) {
- self.nextState = nextState
- self.output = output
- }
- }
- }
- extension ServerInterceptorStateMachine.NextStateAndOutput where Output == Void {
- internal init(nextState: NextState) {
- self.nextState = nextState
- self.output = ()
- }
- }
- extension ServerInterceptorStateMachine.Intercepting {
- /// States which can be reached directly from 'Intercepting'.
- @usableFromInline
- internal struct NextState {
- @usableFromInline
- let state: ServerInterceptorStateMachine.State
- @inlinable
- init(_state: ServerInterceptorStateMachine.State) {
- self.state = _state
- }
- @usableFromInline
- internal static func intercepting(_ state: ServerInterceptorStateMachine.Intercepting) -> Self {
- return Self(_state: .intercepting(state))
- }
- @usableFromInline
- internal static func finished(from: ServerInterceptorStateMachine.Intercepting) -> Self {
- return Self(_state: .finished(.init(from: from)))
- }
- }
- }
- extension ServerInterceptorStateMachine.Finished {
- /// States which can be reached directly from 'Finished'.
- @usableFromInline
- internal struct NextState {
- @usableFromInline
- let state: ServerInterceptorStateMachine.State
- @inlinable
- internal init(_state: ServerInterceptorStateMachine.State) {
- self.state = _state
- }
- @usableFromInline
- internal static func finished(_ state: ServerInterceptorStateMachine.Finished) -> Self {
- return Self(_state: .finished(state))
- }
- }
- }
- #endif // compiler(>=5.6)
|