Skip to content

Allow FitnessObserver::observe and Repopulator::repopulate to take &mut self#139

Merged
HyperCodec merged 3 commits intodevfrom
copilot/allow-fitnessobserver-mutable-reference
Mar 2, 2026
Merged

Allow FitnessObserver::observe and Repopulator::repopulate to take &mut self#139
HyperCodec merged 3 commits intodevfrom
copilot/allow-fitnessobserver-mutable-reference

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

FitnessObserver::observe and Repopulator::repopulate previously took &self, forcing implementations that need to track state to use interior mutability (Cell, Mutex, etc.). Since these are not shared across threads, &mut self is sufficient and more ergonomic.

Changes

  • FitnessObserver::observe: &self&mut self
  • Eliminator::eliminate: &self&mut self (required to propagate mutability through FitnessEliminator::eliminateself.observer.observe(...))
  • Repopulator::repopulate: &self&mut self
  • All implementations updated: (), LayeredObserver, FitnessEliminator, KnockoutEliminator, MitosisRepopulator, CrossoverRepopulator, SpeciatedCrossoverRepopulator
  • Tests updated: CountingObserver impl and mut layered bindings

Example

Observers and repopulators can now hold mutable state directly without interior mutability:

struct LoggingObserver {
    count: usize,
    history: Vec<f32>,
}

impl<G> FitnessObserver<G> for LoggingObserver {
    fn observe(&mut self, fitnesses: &[(G, f32)]) {
        self.count += 1;
        if let Some((_, best)) = fitnesses.first() {
            self.history.push(*best);
        }
    }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Allow FitnessObserver to take a mutable reference</issue_title>
<issue_description>It's not being sent across threads or anything, so there's really no complications or anything preventing us from passing a mutable reference. Makes maintaining observers easier since virtually every case needs to mutate some internal state.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: HyperCodec <72839119+HyperCodec@users.noreply.github.com>
Copilot AI changed the title [WIP] Allow FitnessObserver to take a mutable reference Allow FitnessObserver::observe to take &mut self Mar 2, 2026
@HyperCodec
Copy link
Owner

@copilot do the same for repopulators for consistency.

Co-authored-by: HyperCodec <72839119+HyperCodec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot do the same for repopulators for consistency.

Done in 81b38a1. Repopulator::repopulate and all three implementations (MitosisRepopulator, CrossoverRepopulator, SpeciatedCrossoverRepopulator) now take &mut self.

Copilot AI changed the title Allow FitnessObserver::observe to take &mut self Allow FitnessObserver::observe and Repopulator::repopulate to take &mut self Mar 2, 2026
@HyperCodec HyperCodec marked this pull request as ready for review March 2, 2026 16:29
@HyperCodec HyperCodec merged commit 4b17034 into dev Mar 2, 2026
3 checks passed
@HyperCodec HyperCodec deleted the copilot/allow-fitnessobserver-mutable-reference branch March 2, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow FitnessObserver to take a mutable reference

2 participants