chiark / gitweb /
Bug in conversion from slice
authorSimon Tatham <anakin@pobox.com>
Sun, 13 Apr 2025 07:02:53 +0000 (08:02 +0100)
committerSimon Tatham <anakin@pobox.com>
Sun, 13 Apr 2025 07:02:53 +0000 (08:02 +0100)
I forgot the analogue of truncating the Vec. But here we can truncate
while it's still in cheap slice form, before making a Vec at all.

src/finitenimber.rs

index cb76756c95fbfa93cb5537c8500a97f24c5c34a6..782ff21b1d037de1f41aea2408f30b546af98add 100644 (file)
@@ -284,15 +284,17 @@ impl From<Vec<Word>> for FiniteNimber {
 
 impl From<&[Word]> for FiniteNimber {
     fn from(slice: &[Word]) -> FiniteNimber {
-        match slice
+        match dbg!(slice
             .iter()
             .enumerate()
             .rev()
-            .find(|(_index, word)| **word != 0)
+            .find(|(_index, word)| **word != 0))
         {
             None => Self(FiniteNimberEnum::Single(0)),
             Some((0, w)) => Self(FiniteNimberEnum::Single(*w)),
-            Some(_) => Self(FiniteNimberEnum::Vector(slice.into()))
+            Some((pos, _)) => {
+                Self(FiniteNimberEnum::Vector(slice[..=pos].into()))
+            }
         }
     }
 }