Repository: coreylowman/dfdx (language: rust)

Issue:
Bug: `as_vec` crashes for empty Tensors
Code:

```rust
let dev = Cpu::default();
let x: Tensor<(usize,), f32, Cpu> = dev.ones_like(&(0,));
// This line crashes.
x.as_vec();
```

Crash message:

```
Message:  index out of bounds: the len is 0 but the index is 0
Location: dfdx/src/tensor/cpu/iterate.rs:173
```



Discussion / hints:

Fix patch:
diff --git a/src/tensor/cpu/iterate.rs b/src/tensor/cpu/iterate.rs
index b666528c0..f3cdcd0cd 100644
--- a/src/tensor/cpu/iterate.rs
+++ b/src/tensor/cpu/iterate.rs
@@ -18,7 +18,11 @@ impl<S: Shape> NdIndex<S> {
             indices: Default::default(),
             shape: shape.concrete(),
             strides,
-            next: Some(0),
+            next: if shape.num_elements() > 0 {
+                Some(0)
+            } else {
+                None
+            },
             contiguous: (strides == shape.strides()).then(|| shape.num_elements()),
         }
     }


Test patch: