Repository: starkware-libs/cairo (language: rust)

Issue:
feat: impl `IntoIterator` on `ByteArray`
# Feature Request

**Describe the Feature Request**

In the corelib.
I'm under the impression `ByteArray` doesn't implement IntoIterator, making it impossible to loop over using the language loop syntax.
But it implement index and len. So I think it could be possible to impl IntoIterator too. Is there a reason we didn't do it

I think it makes sense to impl `IntoIterator<felt252>` where the pending word is returned last, as it is, without any indication of `pending_word_len`


**If the feature request is approved, would you be willing to submit a PR?**
_(Help can be provided if you need assistance submitting a PR)_

- [ ] Yes
- [x] No



Discussion / hints:
We should also implement the hash trait:

Is it `h(data0, .., dataN, pending_word, pending_word_len)`?

And the serde trait

i can work on this issue

Fix patch:
diff --git a/corelib/src/byte_array.cairo b/corelib/src/byte_array.cairo
index d16aab73625..394ecdb682c 100644
--- a/corelib/src/byte_array.cairo
+++ b/corelib/src/byte_array.cairo
@@ -490,3 +490,26 @@ pub(crate) impl ByteArrayIndexView of crate::traits::IndexView<ByteArray, usize,
         self.at(index).expect('Index out of bounds')
     }
 }
+
+// TODO: Implement a more efficient version of this iterator.
+/// An iterator struct over a ByteArray.
+#[derive(Drop, Clone)]
+pub struct ByteArrayIter {
+    ba: ByteArray,
+    current_index: crate::ops::RangeIterator<usize>,
+}
+
+impl ByteArrayIterator of crate::iter::Iterator<ByteArrayIter> {
+    type Item = u8;
+    fn next(ref self: ByteArrayIter) -> Option<u8> {
+        self.ba.at(self.current_index.next()?)
+    }
+}
+
+impl ByteArrayIntoIterator of crate::iter::IntoIterator<ByteArray> {
+    type IntoIter = ByteArrayIter;
+    #[inline]
+    fn into_iter(self: ByteArray) -> ByteArrayIter {
+        ByteArrayIter { current_index: (0..self.len()).into_iter(), ba: self }
+    }
+}
diff --git a/corelib/src/ops.cairo b/corelib/src/ops.cairo
index ef8359143fa..d2200838bf7 100644
--- a/corelib/src/ops.cairo
+++ b/corelib/src/ops.cairo
@@ -11,7 +11,7 @@ mod range;
 // `RangeOp` is used internally by the compiler.
 #[allow(unused_imports)]
 use range::RangeOp;
-pub use range::{Range, RangeTrait};
+pub use range::{Range, RangeIterator, RangeTrait};
 
 mod function;
 pub use function::{Fn, FnOnce};


Test patch:
diff --git a/corelib/src/test/byte_array_test.cairo b/corelib/src/test/byte_array_test.cairo
index 12f56e27cc4..53796d20ae8 100644
--- a/corelib/src/test/byte_array_test.cairo
+++ b/corelib/src/test/byte_array_test.cairo
@@ -1,3 +1,4 @@
+use crate::iter::{IntoIterator, Iterator};
 use crate::test::test_utils::{assert_eq, assert_ne};
 
 #[test]
@@ -480,6 +481,18 @@ fn test_serde() {
     );
 }
 
+#[test]
+fn test_iterator() {
+    let ba: ByteArray = "hello";
+    let mut iter = ba.into_iter();
+    assert_eq!(iter.next(), Option::Some('h'));
+    assert_eq!(iter.next(), Option::Some('e'));
+    assert_eq!(iter.next(), Option::Some('l'));
+    assert_eq!(iter.next(), Option::Some('l'));
+    assert_eq!(iter.next(), Option::Some('o'));
+    assert_eq!(iter.next(), Option::None);
+}
+
 // ========= Test helper functions =========
 
 fn compare_byte_array(