From a76dcbb67d4f6ae402f03d22e8d526408d8d875f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kat=20March=C3=A1n?= <kzm@zkat.tech>
Date: Fri, 14 Jan 2022 22:19:23 -0800
Subject: [PATCH] fix(choice): return &Choice instead of cloning

---
 src/pickers.rs | 6 +++---
 src/thinker.rs | 9 ++-------
 2 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/src/pickers.rs b/src/pickers.rs
index a83e160..abb0b02 100644
--- a/src/pickers.rs
+++ b/src/pickers.rs
@@ -12,7 +12,7 @@ Required trait for Pickers. A Picker is given a slice of choices and a query tha
 Implementations of `pick` must return `Some(Choice)` for the `Choice` that was picked, or `None`.
  */
 pub trait Picker: std::fmt::Debug + Sync + Send {
-    fn pick(&self, _choices: &[Choice], _utilities: &Query<&Score>) -> Option<Choice>;
+    fn pick<'a>(&self, choices: &'a [Choice], scores: &Query<&Score>) -> Option<&'a Choice>;
 }
 
 /**
@@ -38,11 +38,11 @@ impl FirstToScore {
 }
 
 impl Picker for FirstToScore {
-    fn pick(&self, choices: &[Choice], scores: &Query<&Score>) -> Option<Choice> {
+    fn pick<'a>(&self, choices: &'a [Choice], scores: &Query<&Score>) -> Option<&'a Choice> {
         for choice in choices {
             let value = choice.calculate(scores);
             if value >= self.threshold {
-                return Some(choice.clone());
+                return Some(choice);
             }
         }
         None
diff --git a/src/thinker.rs b/src/thinker.rs
index bc01bd0..45aa7c8 100644
--- a/src/thinker.rs
+++ b/src/thinker.rs
@@ -241,13 +241,8 @@ pub fn thinker_system(
                     // Think about what action we're supposed to be taking. We do this
                     // every tick, because we might change our mind.
                     // ...and then execute it (details below).
-                    exec_picked_action(
-                        &mut cmd,
-                        *actor,
-                        &mut thinker,
-                        &choice.action,
-                        &mut action_states,
-                    );
+                    let action = choice.action.clone();
+                    exec_picked_action(&mut cmd, *actor, &mut thinker, &action, &mut action_states);
                 } else if let Some(default_action_ent) = &thinker.otherwise {
                     // Otherwise, let's just execute the default one! (if it's there)
                     let default_action_ent = default_action_ent.clone();
-- 
GitLab