Browse Source

Add upper bound on randomization attempts

Kevin Lee 3 months ago
parent
commit
cf75b3bcf4
1 changed files with 9 additions and 1 deletions
  1. 9 1
      src/config/pen.rs

+ 9 - 1
src/config/pen.rs

@@ -366,6 +366,7 @@ impl RandomizePen {
         );
 
         let mut generated: Vec<RandomizedSetting> = vec![];
+        const MAX_ATTEMPTS: usize = 1000;
 
         for (index, pen) in pens
             .iter_mut()
@@ -373,7 +374,7 @@ impl RandomizePen {
             .take(self.count)
             .enumerate()
         {
-            loop {
+            for attempt in 0..=MAX_ATTEMPTS {
                 let mut setting: RandomizedSetting = RandomizedSetting::default();
 
                 if let Some((min, max, step)) = self.speed {
@@ -430,6 +431,13 @@ impl RandomizePen {
                 } else {
                     debug!("Duplicate random setting");
                 }
+
+                if attempt == MAX_ATTEMPTS {
+                    panic!(
+                        "Exceeded maximum number of {} randommization attempts",
+                        MAX_ATTEMPTS
+                    );
+                }
             }
 
             pen.valid_settings();