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
Replace System with FnMut #7
Comments
@msiglreith I highly support making this more convenient, but this is actually not that easy at the moment because the |
This issue could be solved with Associated Type Constructors (it would be a benefit for the whole crate). @msiglreith If you have any ideas, please don't hesitate to comment! |
@torkleyy I'll give it a try later on! |
Hm, indeed quite challenging! |
It seems we cannot solve this at the moment. Let's come back to it when we have HKTs. I plan to publish a new version in the next days (fyi, in case anybody wants to make changes). |
I don't think is a good idea. Many systems have some form of persistent state they need to maintain, e.g. a rendering system might contain the renderer itself, a window handle, and the scene representation, while an audio system would contain the audio sink and context. Where would these be stored if systems were only functions? Would they be stored directly within the engine? If so, that seems to run counter to the idea of systems in the ECS sense (each system contains all the logic necessary to handle a particular task and should be as self-contained as possible). I get that the code examples look pretty verbose with all these empty structs and boilerplate, but if you're implementing any non-trivial system, functions simply cannot suffice. |
I think this change would be good if and when implementing Can we blanket impl |
No, we cannot; the parameters of |
@ebkalderon The shown example might be not optimal but the but approaches are equally powerful. A bit independent from the topic:
I kinda disagree here as storing data in system vs data in resources mainly differs in the fact that the first one is private and the other one can be accessed by multiple systems. I would argue that shared access is more common. |
Oh wow, much controversial, so arguments! 🔫 ✌️ @msiglreith
Could you elaborate further? Let's see if the technical limitations are truly blocking us here. @ebkalderon |
@kvark Sure: Let's first look at the source code for pub trait FnMut<Args>: FnOnce<Args> {
fn call_mut(&mut self, args: Args) -> Self::Output;
} As you can see, impl<T, U> System for T where T: FnMut(U) {
type SystemData = U;
fn run(&mut self, u: U) {
self(u);
}
} right? Now, let's revise the two core difference between an associated type ( struct Foo;
impl WithParam<i32> for Foo {}
impl WithParam<String> for Foo {} // Why not?
impl WithAssoc for Foo { type A = i32; }
impl WithAssoc for Foo { type A = String; } // Nope As you can see, you can only have a single associated type for a given type, while type parameters don't have such a restriction. Going from that, a type I hope you could follow what I'm trying to explain here. If you want to, you can also see the error in this Playground example I created. |
@kvark Fair enough, but could something like a GL context be safely stored as a resource? You can specify systems to be thread-local to the |
Hmm, don't we provide the argument when scheduling a system? In this case, only the @ebkalderon that appears to be a very strong concern, and I don't see how a GL context can be a resource. Maybe we can have |
I think prohibiting |
See #45 for the solution |
I think we can close this. The original issue seems to be resolved and the current discussion is off topic. |
The current code for defining a new task looks like this (from the README)
Every system needs an additional struct (often unit type from my own experience) which becomes quite verbose. Alternative might be replace the Task trait with a function:
The dispatch builder would then take a
F: FnMut(&mut T, U), U: SystemData
(or something similar).I'm not 100% sure if this will be possible, tests some time ago looked positive..
The text was updated successfully, but these errors were encountered: