Skip to content
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

Make gloo::timer's future-based design like stdlib #162

Closed
futursolo opened this issue Oct 31, 2021 · 2 comments · Fixed by #163
Closed

Make gloo::timer's future-based design like stdlib #162

futursolo opened this issue Oct 31, 2021 · 2 comments · Fixed by #163
Labels
enhancement New feature or request

Comments

@futursolo
Copy link
Collaborator

futursolo commented Oct 31, 2021

Summary

Make gloo::timer's future-based API like std / tokio.

Motivation

Why are we doing this? What problems does it solve?

This issue purposes to make gloo::timer's design more like Rust's standard library.

There was a similar discussion for the tokio library years ago about making the Tokio's sleep / timeout design to be similar to the standard library's design.

tokio-rs/tokio#2770
tokio-rs/tokio#2826

The rationale:

  • It's more familiar with people coming from other Rust areas.
  • The std::thread::sleep takes a Duration which is a better design than taking a number in millisecond in my opinion.
  • gloo::timer::sleep(Duration::from_secs(1)).await is easier to understand and remember than gloo::timer::TimeoutFuture::new(1000).await for most people.

Detailed Explanation

async fn sleep(duration: Duration) {
    // ...
}

Drawbacks, Rationale, and Alternatives

Does this design have drawbacks?

It breaks compatibility.

Are there alternative approaches?

The current approach of gloo::timer.

Why is this design the best of all designs available?

This is a design that meets the expectation for people coming from both Rust and other common programming languages.

What prior art exists?

This is the design used by std and tokio.

@futursolo futursolo changed the title Make gloo::times Make gloo::timer 's design like stdlib Oct 31, 2021
@futursolo futursolo changed the title Make gloo::timer 's design like stdlib Make gloo::timer's future-based design like stdlib Oct 31, 2021
@futursolo
Copy link
Collaborator Author

Sorry for filing an incomplete issue. I have updated the issue title and description.

@hamza1311
Copy link
Collaborator

I like this approach. How about we add a function for sleep in addition to the current API?

fn sleep(duration: Duration) -> TimeoutFuture {
    // ...
}

// somewhere else
sleep(Duration::from_secs(5)).await
// after 5 seconds 

@hamza1311 hamza1311 added the enhancement New feature or request label Oct 31, 2021
@futursolo futursolo mentioned this issue Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants