1

I'm working on an ECS system that automatically multithreads systems (similar to bevvy). However sometimes third party libraries don't play nice with multithreading. Bevvy's answer is resources, but for me I'd like to allow my components (structs) to be sync or non-sync. Then if a system requires access to a non-sync struct, it will run on the main thread. An example I ran into early was SDL whose context requires being on the main thread.

My current API works something like this

world.add_system(my_system) // where my_system implements IntoSystem<TSystem>

The best solution I could come up with is having different methods and types, something like

world.add_single_threaded_system(other_system)

but I really would like to use a single method call to add a system, and have my library determine if that system is also sync. But I'm stuck on what this single method would take as a parameter, and how I could then sort this system into a collection of sync systems that can then be sent to a thread pool, or single threaded systems that would not support that.

So I effectively want something like

fn add_system(system: impl IntoSystem<TSystem>) where TSystem: System + Sync?

I thought about having systems implement traits, but the blanket impls would conflict, since every System + Sync would also be just System.

Any suggestions? Or give up and use a different API?

2
  • 1
    Specialization. ~Impossible (but there might be workarounds). Commented Aug 19 at 21:56
  • That's kinda what I figured... It's not the end of the world to have another api for the exception, but it does slowly creep complexity into the api. The more I reason about it, the more it would be fundamentally against statically type checked software. The way to achieve it would be overloaded functions which rust doesn't have. Commented Aug 20 at 17:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.