Hey friends,
I've been trying to learn Druid and attempted to explore how to effectively structure the application. I finally got it compiling, but it immediately stack overflows.
Minimal Example: https://github.com/Deedasmi/druid_overflow
OS: macOS 10.15.6
What I expected to happen (may be way off, still learning Druid):
GameState initializes with next_scene GameScene::Initial. InitialState also initializes with a default next_scene of GameScene::Initial. Druid begins checking (via ViewSwitcher) whether GameState.get_next_scene() is NOT GameScene::Initial and updates the ViewSwitcher accordingly. However, in this example, nothing ever mutates GameState.current_scene, and I would expect the application to load on "Initial" and break if you clicked any buttons.
What actually happens:
Application stack overflows without ever rendering a window.
Notes:
I realize this structure is flawed as I can't see a way to mutate GameState.current_scene in the context of the view switcher, but I still can't see any reason it would stack overflow, so I figured it was worth a report.
I'm really not sure what's going on here, but my first suspicion would be that there's a recursive data structure somewhere? I don't _think_ this should be in druid code, although skimming your example I don't see anything obvious.
ping @Finnerale, It sounds similar why you decided close #751
Maybe the problem with the ViewSwitcher:
No this is not ViewSwitchers fault. The problem is this:
impl Default for LoginState {
fn default() -> Self {
LoginState {
next_scene: GameScene::Login,
.. Default::default()
}
}
}
Calling default recursively.
Huh, apparently I don't know exactly how .. Default::default() works. Thought it just called default on all currently undefined fields, which had no recursion. That did fix it though. Thanks!
@Deedasmi Well basically
impl Default for LoginState {
fn default() -> Self {
LoginState {
next_scene: GameScene::Login,
.. Default::default()
}
}
}
is the same as
impl Default for LoginState {
fn default() -> Self {
LoginState {
next_scene: GameScene::Login,
.. LoginState::default()
}
}
}
where all missing fields (e.g. all except next_scene) will be taken from LoginState::default().
I hope that makes it a bit clearer.
Oh, so it instantiates and moves. I assumed it would enumerate identities. That does help, thanks!
Most helpful comment
No this is not
ViewSwitchers fault. The problem is this:Calling
defaultrecursively.