Eliminate NonSend
resources by making World
optionally !Send
using generics
#17517
Labels
A-ECS
Entities, components, systems, and events
C-Feature
A new feature, making something new possible
D-Complex
Quite challenging from either a design or technical perspective. Ask for help!
S-Ready-For-Implementation
This issue is ready for an implementation PR. Go for it!
X-Blessed
Has a large architectural impact or tradeoffs, but the design has been endorsed by decision makers
What problem does this solve or what need does it fill?
NonSend
resources are confusing (they don't implResource
!), badly named (#13481), require API duplication and hamper us when implementing valuable new ECS features like #17485.That said, not everything we might want to stick in and access from the ECS is threadsafe! Audio, window handles, scripting language integrations and more are all inherently !Send. Simply removing them is not an option.
Ultimately, the problem is that our model here is muddy, hampering our ability to teach, reason about and safely improve these areas of code.
Simultaneously, users have requested support for
!Send
components, particularly for use with web or scripting languages.What solution would you like?
As discussed in the resources as entities Discord thread (and proposed by @bushrat011899, we should support
!Send
World
s.We already have a powerful, type-erased storage. We should take advantage of it, expanding the possibilities for users while clarifying the semantics and ensuring that our objects are either clearly
Send
or!Send
.PR 0: Send-generic World
To do this, we can add a generic parameter to
World
, which conditionally holds aPhantomData
which makes it !Send. See this playground link for a proof of concept. To make that nice, we probably want a sealed trait, meaningful names and an associated constant on that trait that you can check at runtime. Steal from the immutable components design.PR 1: Move
Send+Sync
trait boundsCurrently, the
Resource
andComponent
traits require their types to be bothSend
andSync
. In this brave new world, this is no longer required! Nonsend resources can be actual resources! We should remove those bounds, and add them to the dangerous methods (hi parallel executor + parallel query iteration) which actually require them.PR 2:
App
stores a!Send
worldAs laid out by @maniwani, make all apps regardless of platform would come with a world (thread-safe world) and a centralized thread-local storage (thread-local world).
PR 3: Thread-safe worlds can access thread-local data
Allow systems in the normal world to access non-send data, stored in the thread-local storage. Normal systems never directly touch non-send data (except on web). The mechanism will look something like this.
The thread-local world should live to service requests from the thread-safe (main) world, although users with particularly unusual needs could operate with only a thread-local world or add and run systems to it.
PR 4: Migrate internals to new system
Move all of Bevy's
NonSend
usages to the new strategy, and make sure everything works.PR 5: yeet
NonSend
Remove all of the internal now-redundant machinery that is currently used to implement !Send resources.
Future work
What alternative(s) have you considered?
See the following previous attempts at this flavor of work:
impl Send for World
#3519Send
resources out ofWorld
#9122This solution is powerful, elegant and minimizes API duplication.
Using a const boolean would work, as seen in this playground proof of concept, but results in uglier and less explicit APIs.
Once
const-adt
s is stabilized, moving to a custom enum will give us a simpler, cleaner solution without sacrificing clarity.The text was updated successfully, but these errors were encountered: