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

refactor(rt): Clean up Timer trait #3037

Merged
merged 2 commits into from Dec 13, 2022
Merged

Conversation

sfackler
Copy link
Contributor

This both avoids an unnecessary extra layer of boxing and removes some dubious unsafe code.

Closes #3028

@@ -20,16 +20,16 @@ pub trait Executor<Fut> {
/// A timer which provides timer-like functions.
pub trait Timer {
Copy link

Choose a reason for hiding this comment

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

Unrelated: Would it be good to have the resulting futures as an associated type rather than as a boxed future? Both tokio and smol have well-defined "sleep" types instead of async methods, so we may be able to avoid an allocation here by not boxing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would require parameterizing the ~entire API over the timer trait which I'm not sure is desired.

Copy link

Choose a reason for hiding this comment

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

As a workaround, you could just do:

struct BoxedTimer {
    inner: Box<dyn Timer<Sleep = Pin<Box<dyn Future>>>>>
}

But this might not be idiomatic, so I agree with your assessment.

@bossmc
Copy link
Contributor

bossmc commented Nov 3, 2022

Is there an assumption in hyper that the Sleep future(s) cancel on drop? I could imagine a fairly simple implementation (a BinaryHeap of wakers) where sleep-futures continue to fire their wakers after the future has been dropped - would this be a problem?

If this is expected, it would be nice to specify in the trait comment so implementors get this right.

@sfackler
Copy link
Contributor Author

sfackler commented Nov 3, 2022

Any Future implementation needs to work in the presence of polls that don't make progress, so I can't see how hyper could make that assumption.

@bossmc
Copy link
Contributor

bossmc commented Nov 3, 2022

Fair enough, deregistering/cancelling the timer on Drop of the Sleep future is therefore simply an optimization (to reduce unnecessary wakeups) but not needed for correctness, thanks for confirming!

@seanmonstar seanmonstar merged commit 8790fee into hyperium:master Dec 13, 2022
@sfackler sfackler deleted the timer-pin branch December 15, 2022 20:20
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.

Timer methods should return Pin<Box<dyn Sleep>> and Sleep should drop the Unpin requirement
4 participants