func canonicalize0()

in Sources/DistributedActors/Behaviors.swift [845:896]


        func canonicalize0(_ context: _ActorContext<Message>, base: _Behavior<Message>, next: _Behavior<Message>, depth: Int) throws -> _Behavior<Message> {
            let deeper = depth + 1
            guard depth < failAtDepth else {
                throw IllegalBehaviorError.tooDeeplyNestedBehavior(reached: next, depth: depth)
            }

            var canonical = next
            while true {
                switch canonical.underlying {
                case .setup(let onStart): canonical = try onStart(context)

                case .same where self.isSetup: throw IllegalBehaviorError.illegalTransition(from: self, to: next)
                case .same: return base
                case .ignore: return base
                case .unhandled: return base

                case .stop(.none, let reason): return .stop(postStop: base, reason: reason)
                case .stop(.some, _): return canonical
                case .failed: return canonical

                case .orElse(let first, let second):
                    let canonicalFirst = try canonicalize0(context, base: canonical, next: first, depth: deeper)
                    let canonicalSecond = try canonicalize0(context, base: canonical, next: second, depth: deeper)
                    return canonicalFirst.orElse(canonicalSecond)

                case .intercept(let inner, let interceptor):
                    let innerCanonicalized: _Behavior<Message> = try canonicalize0(context, base: inner, next: .same, depth: deeper)
                    return .intercept(behavior: innerCanonicalized, with: interceptor)

                case .signalHandling(let onMessage, let onSignal):
                    return .signalHandling(
                        handleMessage: try canonicalize0(context, base: canonical, next: onMessage, depth: deeper),
                        handleSignal: onSignal
                    )
                case .signalHandlingAsync(let onMessage, let onSignalAsync):
                    return .signalHandlingAsync(
                        handleMessage: try canonicalize0(context, base: canonical, next: onMessage, depth: deeper),
                        handleSignal: onSignalAsync
                    )

                case .suspend(let handler):
                    return .suspended(previousBehavior: base, handler: handler)
                case .suspended:
                    return canonical

                case .receive, .receiveMessage:
                    return canonical
                case .receiveAsync, .receiveMessageAsync:
                    return canonical
                }
            }
        }