-
-
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
Consider adding spawn_unchecked for non-static futures #2385
Comments
Note that because of the fact that futures can be cancelled at any time, doing this while correctly handling cancellation becomes much more difficult compared to ordinary scoped threads. See more details in #1879. |
This sounds like something dangerous but could probably be implemented externally? You can write an extension trait that provides this. |
This would be an equivalent to: https://doc.rust-lang.org/std/thread/struct.Builder.html#method.spawn_unchecked |
@rnarubin as a word of warning, |
@LucioFranco Implementing this functionality externally necessitates the allocations and dynamic dispatch mentioned in the OP, as that's the only way to convert a non- I think this is worth having for the same reason as in Would you be open to a PR that adds |
Thanks for the suggestion. We discussed this again recently. We are opting to not add this function, at least for the time being.
Once |
Currently the various
spawn
functions require the provided future to have a'static
lifetime, in much the same way thatstd::thread::spawn
requires the passed closure to have a'static
bound. This is because there's nothing constraining the future from outliving its originating scope, so for soundness it should not have any shorter borrows.In theory, however, there are ways to constrain the lifespan of a future (e.g. closure scopes and forceful cancellation) such that it would be safe to borrow from a non-
'static
scope. For libraries exploring this design space, the only current workaround to spawn such a future is to box it as a trait object and then transmute to the static lifetime:Although this works, it requires both an allocation and dynamic dispatch (where the underlying scheduler will also add similar indirection). This is even more complicated when the
Output
type is non-'static
, which is a can of worms I won't open here for brevity.I propose adding an unsafe
spawn_unchecked
function to theHandle
type with a signature like this:This is similar to the proposal for adding such a function to
std::thread
with exactly the same motivation: allowing libraries to build scoped-spawn functionality without the overhead of lifetime workarounds.The text was updated successfully, but these errors were encountered: