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

Code before:
    @inlinable
    public func scheduleCallback(
        in amount: TimeAmount,
        handler: some (NIOScheduledCallbackHandler & Sendable)
    ) throws -> NIOScheduledCallback {
        try self.scheduleCallback(at: .now() + amount, handler: handler)
    }

    /// Default implementation of `cancelScheduledCallback(_:)`: only cancels callbacks scheduled by the default implementation of `scheduleCallback`.
    ///
    /// - NOTE: Event loops that provide a custom scheduled callback implementation **must** implement _both_
    ///         `sheduleCallback(at deadline:handler:)` _and_ `cancelScheduledCallback(_:)`. Failure to do so will
    ///         result in a runtime error.
    @inlinable
    public func cancelScheduledCallback(_ scheduledCallback: NIOScheduledCallback) {
        switch scheduledCallback.backing {
        case .default(let task):
            task.cancel()
        case .custom:
            preconditionFailure("EventLoop missing custom implementation of cancelScheduledCallback(_:)")
        }
    }
}

@usableFromInline
struct UnsafeUncheckedScheduledCallbackHandlerWrapper<Handler: NIOScheduledCallbackHandler>: @unchecked Sendable, NIOScheduledCallbackHandler {
    private let wrapped: Handler

    @usableFromInline
    init(wrapping handler: Handler) {
        self.wrapped = handler
    }

    @usableFromInline
    func handleScheduledCallback(eventLoop: some EventLoop) {
        self.wrapped.handleScheduledCallback(eventLoop: eventLoop)
    }

    @usableFromInline
    func didCancelScheduledCallback(eventLoop: some EventLoop) {
        self.wrapped.didCancelScheduledCallback(eventLoop: eventLoop)
    }
}


Review comment (suggestion):
Instead of using `@unchecked Sendable` we should use a NIOLoopBound here instead.

Code after:
    @inlinable
    public func scheduleCallback(
        in amount: TimeAmount,
        handler: some (NIOScheduledCallbackHandler & Sendable)
    ) throws -> NIOScheduledCallback {
        try self.scheduleCallback(at: .now() + amount, handler: handler)
    }

    /// Default implementation of `cancelScheduledCallback(_:)`: only cancels callbacks scheduled by the default implementation of `scheduleCallback`.
    ///
    /// - NOTE: Event loops that provide a custom scheduled callback implementation **must** implement _both_
    ///         `sheduleCallback(at deadline:handler:)` _and_ `cancelScheduledCallback(_:)`. Failure to do so will
    ///         result in a runtime error.
    @inlinable
    public func cancelScheduledCallback(_ scheduledCallback: NIOScheduledCallback) {
        switch scheduledCallback.backing {
        case .default(let task):
            task.cancel()
        case .custom:
            preconditionFailure("EventLoop missing custom implementation of cancelScheduledCallback(_:)")
        }
    }
}

@usableFromInline
struct LoopBoundScheduledCallbackHandlerWrapper<Handler: NIOScheduledCallbackHandler>:
    NIOScheduledCallbackHandler, Sendable
{
    private let box: NIOLoopBound<Handler>

    @usableFromInline
    init(wrapping handler: Handler, eventLoop: some EventLoop) {
        self.box = .init(handler, eventLoop: eventLoop)
    }

    @usableFromInline
    func handleScheduledCallback(eventLoop: some EventLoop) {
        self.box.value.handleScheduledCallback(eventLoop: eventLoop)
    }

    @usableFromInline
    func didCancelScheduledCallback(eventLoop: some EventLoop) {
        self.box.value.didCancelScheduledCallback(eventLoop: eventLoop)
    }
}