util: add task builder API #7180
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
This PR implements a builder API for
tokio_util::task::JoinMapas discussed in #7179.Solution
JoinMapnow has a publicbuild_taskmethod that returns aJoinMapBuilder.JoinMapBuilderlets you configure a name for the task and then spawn it on theJoinMapyou originally got the builder from.Because
JoinMapcontains aJoinSetandjoin_set::Builderborrows itsJoinSetmutably,join_map::Buildercannot contain both a&mut JoinMap(which is required for inserting the task) and ajoin_set::Builder(which would borrowJoinMap::tasks). I see 2 solutions to this:JoinMap::insertin such a way that it is a free function instead of a method, and then makeBuildercontain the individual parts ofJoinMapthat are requiredBuilderstore all the same state thatjoin_set::Builder(ortask::Builderfor that matter) storesI chose to go with option 2, although in the future it may be better to switch to option 1, if
task::Builderever extends its API with callbacks and such.The
tokio_util::task::join_mapmodule is currently also not public, so instead of mirroringjoin_map::Builder(where thejoin_mapmodule is public), theBuilderstruct is re-exported asJoinMapBuilder. I'm not convinced this is the way to go, but making the module public would also require documenting it (which I am willing to do if desired).