Skip to content

Adds sleep(for:) to clock #61222

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

Merged
merged 1 commit into from
Jan 5, 2023
Merged

Conversation

mbrandonw
Copy link
Contributor

This adds a duration-based sleep(for:) method to Clock as an alternative to using the instant-based sleep(until:) method.

Evolution proposal coming next, and here's a forum discussion.

@mbrandonw mbrandonw marked this pull request as draft September 21, 2022 12:47
@mbrandonw mbrandonw force-pushed the clock-sleep-for branch 2 times, most recently from 9c42538 to 6b17d53 Compare September 28, 2022 20:59
@benrimmington
Copy link
Contributor

I think you'll need to rebase, so that the #else branch can be updated. Alternatively, you could move the Task.sleep methods to a separate pull request. All changes must be cherry-picked for the release/5.8 branch.

For consistency — and to avoid confusion with Swift.Duration — should new APIs continue to use Instant.Duration, rather than the new Duration associated type?

@mbrandonw mbrandonw force-pushed the clock-sleep-for branch 3 times, most recently from 5d49f47 to 8c9d19e Compare December 29, 2022 00:08
@mbrandonw
Copy link
Contributor Author

Hi @benrimmington, I updated the PR to reflect the changes you mentioned. Let me know if you rather me move the Task.sleep changes to a separate PR.

@@ -156,8 +156,12 @@ extension Task where Success == Never, Failure == Never {
/// - Parameter duration: The duration to wait.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation comments:

  • canceled vs cancelled?
  • remove or expand - Parameter lists?
  • remove or update on a continuous clock?
  • remove clock: .continuous from example code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • canceled vs cancelled?

These docs were copied from the other sleep methods, and they say "canceled". Perhaps OK to leave as-is and maybe all docs can be updated at some future time?

Copy link
Collaborator

@xwu xwu Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Canceled" is the correct spelling in Apple docs, per longstanding practice and style guides dating back decades.

@mbrandonw
Copy link
Contributor Author

Hi @benrimmington, good catches, thanks! I've updated the PR.

@benrimmington
Copy link
Contributor

@swift-ci Please smoke test

@benrimmington
Copy link
Contributor

preset=stdlib_S_standalone_minimal_macho_x86_64,build,test
@swift-ci Please test with toolchain and preset

@mbrandonw mbrandonw marked this pull request as ready for review January 3, 2023 17:33
@mbrandonw
Copy link
Contributor Author

@benrimmington I just realized that this PR was still in draft mode. I have just marked it as "ready for review." Sorry about that!

Copy link
Contributor

@phausler phausler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation here looks good to me.

@stephentyrone
Copy link
Contributor

Please create another PR for 5.8 as well!

Copy link
Contributor

@stephentyrone stephentyrone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing review pending a discussion about ABI stability of adding a default argument.

@@ -169,13 +172,18 @@ extension Task where Success == Never, Failure == Never {
public static func sleep<C: Clock>(
until deadline: C.Instant,
tolerance: C.Instant.Duration? = nil,
clock: C
clock: C = ContinuousClock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that adding a default argument is not actually possible here, because the symbol that provides the default will not be available in library versions that predate the appearance of the default. @slavapestov, is that right?

If so, you should instead add an @_aEIC overload that does not take a clock parameter and calls this function with clock: ContinuousClock()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misremembering, but I'd thought that it was the longstanding case that default arguments are emitted into the client and (thus) ABI stable to add?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, here we go: #56859 (comment)

@mbrandonw
Copy link
Contributor Author

Please create another PR for 5.8 as well!

Can do, but I'm not sure what that means. Do I just cherry pick these changes to the release/5.8 branch and PR from there?

///
@available(SwiftStdlib 5.7, *)
public static func sleep<C: Clock>(
until deadline: C.Instant,
tolerance: C.Instant.Duration? = nil,
clock: C
clock: C = ContinuousClock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here w.r.t. symbols in the library.

@stephentyrone
Copy link
Contributor

Can do, but I'm not sure what that means. Do I just cherry pick these changes to the release/5.8 branch and PR from there?

Yes, once we resolve the question I just raised, that's exactly the process.

@stephentyrone
Copy link
Contributor

Please go ahead and create a cherry-pick to release/5.8, @mbrandonw.

@stephentyrone stephentyrone merged commit e7fc160 into swiftlang:main Jan 5, 2023
@benrimmington
Copy link
Contributor

@mbrandonw Do you need any help with the release/5.8 pull request?

@mbrandonw mbrandonw deleted the clock-sleep-for branch January 12, 2023 15:58
@mbrandonw
Copy link
Contributor Author

Hi @benrimmington, sorry I didn't even realize this branch was merged. I will create the 5.8 PR right now!

@mbrandonw
Copy link
Contributor Author

@benrimmington done! #62996

@AnthonyLatsis AnthonyLatsis linked an issue Mar 10, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should Clock.sleep(for:) be added?
5 participants