Skip to content

Commit deb73a5

Browse files
author
Gilad Chase
committed
refactor(byte_array): delegate ByteArray iterator into ByteSpan iterator
Faster than current approach, which uses `at`.
1 parent 218a5bd commit deb73a5

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

corelib/src/byte_array.cairo

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,26 +559,25 @@ pub(crate) impl ByteArrayIndexView of crate::traits::IndexView<ByteArray, usize,
559559
}
560560
}
561561

562-
// TODO: Implement a more efficient version of this iterator.
563562
/// An iterator struct over a ByteArray.
564563
#[derive(Drop, Clone)]
565564
pub struct ByteArrayIter {
566-
ba: ByteArray,
567-
current_index: IntoIterator::<crate::ops::Range<usize>>::IntoIter,
565+
inner: ByteSpanIter,
568566
}
569567

570568
impl ByteArrayIterator of crate::iter::Iterator<ByteArrayIter> {
571569
type Item = u8;
570+
#[inline]
572571
fn next(ref self: ByteArrayIter) -> Option<u8> {
573-
self.ba.at(self.current_index.next()?)
572+
self.inner.next()
574573
}
575574
}
576575

577576
impl ByteArrayIntoIterator of crate::iter::IntoIterator<ByteArray> {
578577
type IntoIter = ByteArrayIter;
579578
#[inline]
580579
fn into_iter(self: ByteArray) -> Self::IntoIter {
581-
ByteArrayIter { current_index: (0..self.len()).into_iter(), ba: self }
580+
ByteArrayIter { inner: self.span().into_iter() }
582581
}
583582
}
584583

0 commit comments

Comments
 (0)