-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
util: implement ReusableBoxFuture
with one function
#4675
util: implement ReusableBoxFuture
with one function
#4675
Conversation
This is so we can get an unconditional `Send` bound on rustc <1.60.
By the way, I didn't catch that Clippy warning initially because Tokio doesn't even build for my with my local Clippy. If I try it just produces this error: Click to show Clippy output
Not sure what's going on here but it would be nice if that could be fixed |
Tokio uses an older clippy version because some of the new lints are incompatible with our minimum supported Rust version. You can find the version we use in the CI configuration. |
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.
Overall, this approach looks good to me --- having the ReusableBoxFuture
own a Pin<Box<...>>
definitely feels much nicer than the NonNull
, as we need less unsafe code outside of the code that actually reuses the allocation.
I commented on a few relatively minor things.
{ | ||
// future::Pending<T> is a ZST so this never allocates. | ||
let boxed = mem::replace(&mut this.boxed, Box::pin(Pending(PhantomData))); | ||
reuse_pin_box(boxed, future, |boxed| this.boxed = Pin::from(boxed)) |
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.
this appears to be the only place where reuse_pin_box
is ever called...do you think it would make sense to just put that code in this function? that way we avoid having an additional function that gets monomorphized over a big pile of generic types, which might help with binary size a bit...
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.
Conceptually I quite like the idea of reuse_pin_box
as an independent futures-unaware utility function — it makes a lot of sense to me as a layer of abstraction. Would putting #[inline(always)]
on the function have the same effect?
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.
My guess is that it'll be inlined either way.
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.
Yeah, I think it'll get inlined either way.
I'm not strongly opposed to keeping the separate function; I just wanted to note that it isn't currently used elsewhere. 🤷♀️
## Motivation In #4675 I learnt the hard way that Tokio uses Clippy on its MSRV. ## Solution Document this in the contributor's guide.
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.
Motivation
ReusableBoxFuture
is currently complex-ish, withunsafe
code and types in multiple places.Solution
Move all the
unsafe
ty into a single functionreuse_pin_box
(and a helper type). I think this makes the definition ofReusableBoxFuture
a bit simpler, and I'm pretty sure that it's sound. The function has the signature:It encapsulates the basic premise of
ReusableBoxFuture
— aBox
that can be reused — andReusableBoxFuture
is then implemented on top of it. It accepts a callback instead of returningResult<Box<U>, O>
so that code can be run even if dropping the oldT
fails.