Repository: apple/swift-nio
File: Sources/NIOCore/ChannelPipeline.swift (Swift)

Code before:
        }

        /// Fires `register` from the tail to the head.
        ///
        /// This method should typically only be called by `Channel` implementations directly.
        public func register(promise: EventLoopPromise<Void>?) {
            self.eventLoop.assertInEventLoop()
            self._pipeline.register0(promise: promise)
        }

        /// Fires `triggerUserOutboundEvent` from the tail to the head.
        ///
        /// This method should typically only be called by `Channel` implementations directly.
        public func triggerUserOutboundEvent(_ event: Any, promise: EventLoopPromise<Void>?) {
            self.eventLoop.assertInEventLoop()
            self._pipeline.triggerUserOutboundEvent0(event, promise: promise)
        }

        /// Provides scoped access to the underlying transport, if the channel supports it.
        ///
        /// This is an advanced API for reading or manipulating the underlying transport that backs a channel. Users must
        /// not close the transport or invalidate any invariants that NIO relies upon for the channel operation.
        ///
        /// Not all channels support access to the underlying channel. If the channel does not support this API, the
        /// closure is not called and this function immediately returns `nil`.
        ///
        /// - Parameter body: A closure that takes the underlying transport, if the channel supports this operation.
        /// - Returns: The value returned by the closure, or `nil` if the channel does not expose its transport.
        /// - Throws: If the underlying transport is unavailable, or rethrows any error thrown by the closure.
        @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
        public func withUnsafeTransportIfAvailable<Transport, Result>(
            of _: Transport.Type,
            _ body: (_ transport: Transport) throws -> Result
        ) throws -> Result? {
            self.eventLoop.assertInEventLoop()
            guard let channel = self._pipeline._channel as? any NIOTransportAccessibleChannel<Transport> else {
                return nil
            }
            return try channel.withUnsafeTransport(body)
        }
    }

    /// Returns a view of operations which can be performed synchronously on this pipeline. All
    /// operations **must** be called on the event loop.
    public var syncOperations: SynchronousOperations {
        SynchronousOperations(pipeline: self)
    }
}

@available(*, unavailable)
extension ChannelPipeline.SynchronousOperations: Sendable {}

Review comment (suggestion):
Note: This matches the semantics of `Collection.withContiguousStorageIfAvailable(_:)` w.r.t. to if the type doesn't support the operation, i.e. the closure is not called and the optional generic return type is used to signal that.

Code after:
            self._pipeline.connect0(to: address, promise: promise)
        }

        /// Fires `register` from the tail to the head.
        ///
        /// This method should typically only be called by `Channel` implementations directly.
        public func register(promise: EventLoopPromise<Void>?) {
            self.eventLoop.assertInEventLoop()
            self._pipeline.register0(promise: promise)
        }

        /// Fires `triggerUserOutboundEvent` from the tail to the head.
        ///
        /// This method should typically only be called by `Channel` implementations directly.
        public func triggerUserOutboundEvent(_ event: Any, promise: EventLoopPromise<Void>?) {
            self.eventLoop.assertInEventLoop()
            self._pipeline.triggerUserOutboundEvent0(event, promise: promise)
        }

        /// Provides scoped access to the underlying transport, if the channel supports it.
        ///
        /// This is an advanced API for reading or manipulating the underlying transport that backs a channel. Users
        /// must not close the transport or invalidate any invariants that the channel relies upon for its operation.
        ///
        /// Not all channels support access to the underlying channel. If the channel does not support this API, the
        /// closure is not called and this function immediately returns `nil`.
        ///
        /// Note that you must call this API with an appropriate closure, or otherwise explicitly specify the correct
        /// transport type prarameter, in order for the closure to be run. Calling this function such that the compiler
        /// infers a type for the transport closure parameter that differs from the channel implementation will result
        /// in the closure not being run and this function will return `nil`.
        ///
        /// For example, for socket-based channels, that expose the underlying socket handle:
        ///
        /// ```swift
        /// try channel.pipeline.syncOperations.withUnsafeTransportIfAvailable { transport in
        ///     // This closure is called.
        ///     transport == NIOBSDSocketHandle.invalid
        /// }
        ///
        /// try channel.pipeline.syncOperations.withUnsafeTransportIfAvailable { (_: NIOBSDSocket.Handle) in
        ///     // This closure is called.
        ///     return
        /// }
        ///
        /// try channel.pipeline.syncOperations.withUnsafeTransportIfAvailable(of: NIOBSDSocket.Handle.self) { _ in
        ///     // This closure is called.
        ///     return
        /// }
        ///
        /// try channel.pipeline.syncOperations.withUnsafeTransportIfAvailable {