Skip to content

Commit

Permalink
feedback(unsure): Make setTimer generic over the handler
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjbeaumont committed Jul 8, 2024
1 parent 59a04de commit 06a4ce7
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/NIOCore/EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 359,11 @@ public protocol EventLoop: EventLoopGroup {

/// Set a timer that will call a handler at the given time.
@discardableResult
func setTimer(for deadline: NIODeadline, _ handler: any NIOTimerHandler) -> NIOTimer
func setTimer(for deadline: NIODeadline, _ handler: some NIOTimerHandler) -> NIOTimer

/// Set a timer that will call a handler after a given amount of time.
@discardableResult
func setTimer(for duration: TimeAmount, _ handler: any NIOTimerHandler) -> NIOTimer
func setTimer(for duration: TimeAmount, _ handler: some NIOTimerHandler) -> NIOTimer

/// Cancel a timer.
func cancelTimer(_ timer: NIOTimer)
Expand Down
4 changes: 2 additions & 2 deletions Sources/NIOCore/NIOTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 71,15 @@ public struct NIOTimer: Sendable {
extension EventLoop {
/// Default implementation of `setTimer(for deadline:_:)`, backed by `EventLoop.scheduleTask`.
@discardableResult
public func setTimer(for deadline: NIODeadline, _ handler: any NIOTimerHandler) -> NIOTimer {
public func setTimer(for deadline: NIODeadline, _ handler: some NIOTimerHandler) -> NIOTimer {
let task = self.scheduleTask(deadline: deadline) { handler.timerFired(eventLoop: self) }
return NIOTimer(self, task)
}

/// Default implementation of `setTimer(for duration:_:)`, delegating to `setTimer(for deadline:_:)`.
@discardableResult
@inlinable
public func setTimer(for duration: TimeAmount, _ handler: any NIOTimerHandler) -> NIOTimer {
public func setTimer(for duration: TimeAmount, _ handler: some NIOTimerHandler) -> NIOTimer {
self.setTimer(for: .now() duration, handler)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOEmbedded/AsyncTestingEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 172,7 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
}

@discardableResult
public func setTimer(for duration: TimeAmount, _ handler: any NIOTimerHandler) -> NIOTimer {
public func setTimer(for duration: TimeAmount, _ handler: some NIOTimerHandler) -> NIOTimer {
/// Even though this type does not conform to `CustomTimerImplemenation`, it has a manual clock so we cannot
/// rely on the default implemntation of `setTimer(for duration:_:)`, which computes the deadline for
/// `setTimer(for deadline:_:)` naively using `NIODeadline.now`, but we must use `self.now`.
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOEmbedded/Embedded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 128,7 @@ public final class EmbeddedEventLoop: EventLoop {
}

@discardableResult
public func setTimer(for duration: TimeAmount, _ handler: any NIOTimerHandler) -> NIOTimer {
public func setTimer(for duration: TimeAmount, _ handler: some NIOTimerHandler) -> NIOTimer {
/// Even though this type does not conform to `CustomTimerImplemenation`, it has a manual clock so we cannot
/// rely on the default implemntation of `setTimer(for duration:_:)`, which computes the deadline for
/// `setTimer(for deadline:_:)` naively using `NIODeadline.now`, but we must use `self._now`.
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOPosix/MultiThreadedEventLoopGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 482,7 @@ internal struct ScheduledTask {
}

@usableFromInline
init(id: UInt64, _ handler: any NIOTimerHandler, _ time: NIODeadline) {
init(id: UInt64, _ handler: some NIOTimerHandler, _ time: NIODeadline) {
self.id = id
self.readyTime = time
self.kind = .timer(handler)
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOPosix/SelectableEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 897,7 @@ internal func assertExpression(_ body: () -> Bool) {

extension SelectableEventLoop {
@inlinable
func setTimer(for deadline: NIODeadline, _ handler: any NIOTimerHandler) -> NIOTimer {
func setTimer(for deadline: NIODeadline, _ handler: some NIOTimerHandler) -> NIOTimer {
let taskId = self.scheduledTaskCounter.loadThenWrappingIncrement(ordering: .relaxed)
let task = ScheduledTask(id: taskId, handler, deadline)
try! self._schedule0(.scheduled(task))
Expand Down

0 comments on commit 06a4ce7

Please sign in to comment.