-
Notifications
You must be signed in to change notification settings - Fork 667
Conform TimeAmount to AdditiveArithmetic #1691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Conforms TimeAmount to AdditiveArithmetic. Motivation: TimeAmount does not support -=, +=. Sometimes it is useful to manipulate time amounts when building up a delay and if that iteration fails, we would want to delay += .milliseconds(5) to add 5 milliseconds to our delay and try again. Modifications: Conformed TimeAmount to AdditiveArithmetic: added a static zero property and required operators. Result: TimeAmount conforms to AdditiveArithmetic.
Can one of the admins verify this patch? |
10 similar comments
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Generated hooks to run tests on Linux
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! You’ve added a pair of functions that don’t exist on additive arithmetic and that behave a bit unlike the usual behaviour of +=
and -=
. I think it’d probably be best to remove those two.
Removed duplication of -= and += functions already present in AdditiveArithmetic. Moved declaring zero property to extension.
Removed the functions as suggested to preserve usual behavior of |
Sources/NIO/EventLoop.swift
Outdated
@@ -353,10 +359,18 @@ extension TimeAmount { | |||
public static func + (lhs: TimeAmount, rhs: TimeAmount) -> TimeAmount { | |||
return TimeAmount(lhs.nanoseconds + rhs.nanoseconds) | |||
} | |||
|
|||
public static func += (lhs: inout TimeAmount, rhs: TimeAmount) -> TimeAmount { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, this is my fault, I wasn't clear enough. The signature is: public static func += (lhs: inout TimeAmount, rhs: TimeAmount)
. The function is supposed to assign the result into the lhs
argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this function be required, given that we are conforming to AdditiveArithmetic? Removed in later commit, and passes with test:
func testTimeAmountDoesAddTime() {
var lhs = TimeAmount.milliseconds(0)
lhs += .milliseconds(5)
XCTAssertEqual(lhs, .milliseconds(5))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct that this function isn’t required at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, this patch looks reasonable to me!
@swift-nio-bot test this please |
Sadly it looks like the two functions do need to be implemented in older Swift versions to get the conformance. We need |
Added += and -= functions to support 5.0 and 5.1. Added hooks for tests on Linux.
…swift-nio into patch/time-amount
@swift-nio-bot test this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice patch!
@swift-nio-bot test this please |
Conforms TimeAmount to AdditiveArithmetic, resolves #1690
Motivation:
TimeAmount does not support -=, +=. Sometimes it is useful to manipulate time amounts when building up a delay and if that iteration fails, we would want to delay += .milliseconds(5) to add 5 milliseconds to our delay and try again.
Modifications:
Conformed TimeAmount to AdditiveArithmetic: added a static zero property and required operators.
Result:
TimeAmount conforms to AdditiveArithmetic.