Skip to content

Commit

Permalink
Merge branch 'main' into feat/vec_iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 authored Jan 5, 2025
2 parents 2af5ce4 + c579d8c commit 82d27d2
Show file tree
Hide file tree
Showing 50 changed files with 1,658 additions and 445 deletions.
23 changes: 23 additions & 0 deletions corelib/src/byte_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
11 changes: 9 additions & 2 deletions corelib/src/gas.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Utilities for handling gas in Cairo code.

#[cfg(not(gas: "disabled"))]
use crate::RangeCheck;

Expand All @@ -20,12 +22,15 @@ pub extern type GasBuiltin;
/// Returns `Option::Some(())` if there is sufficient gas to handle the success case, otherwise
/// returns `Option::None`.
///
/// Example:
/// # Examples
///
/// ```
/// // The success branch is the following lines, the failure branch is the `panic` caused by the
/// // `unwrap` call.
/// withdraw_gas().unwrap();
/// ```
///
/// ```
/// // Direct handling of `withdraw_gas`.
/// match withdraw_gas() {
/// Option::Some(()) => success_case(),
Expand All @@ -34,6 +39,7 @@ pub extern type GasBuiltin;
/// ```
#[cfg(not(gas: "disabled"))]
pub extern fn withdraw_gas() -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;

/// Placeholder when gas mechanism is disabled.
#[cfg(gas: "disabled")]
pub fn withdraw_gas() -> Option<()> nopanic {
Expand All @@ -48,13 +54,13 @@ pub fn withdraw_gas() -> Option<()> nopanic {
pub extern fn withdraw_gas_all(
costs: BuiltinCosts,
) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;

/// Placeholder when gas mechanism is disabled.
#[cfg(gas: "disabled")]
pub fn withdraw_gas_all(costs: BuiltinCosts) -> Option<()> nopanic {
Option::Some(())
}


/// Returns unused gas into the gas builtin.
///
/// Useful for cases where different branches take different amounts of gas, but gas withdrawal is
Expand All @@ -64,6 +70,7 @@ pub extern fn redeposit_gas() implicits(GasBuiltin) nopanic;
/// Returns the `BuiltinCosts` table to be used in `withdraw_gas_all`.
#[cfg(not(gas: "disabled"))]
pub extern fn get_builtin_costs() -> BuiltinCosts nopanic;

/// Placeholder when gas mechanism is disabled.
#[cfg(gas: "disabled")]
pub fn get_builtin_costs() -> BuiltinCosts nopanic {
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/internal/bounded_int.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Felt252TryIntoBoundedInt<
const MIN: felt252, const MAX: felt252,
> of TryInto<felt252, BoundedInt<MIN, MAX>> {
fn try_into(self: felt252) -> Option<BoundedInt<MIN, MAX>> {
// Using `downcast` is allowed, since `BoundedInt` itself is not `pub`, and only has few
// Using `downcast` is allowed, since `BoundedInt` itself is not `pub`, and only has a few
// specific `pub` instances, such as `u96`, `ConstZero` and `ConstOne`.
downcast(self)
}
Expand Down
9 changes: 9 additions & 0 deletions corelib/src/iter/traits/iterator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ impl IteratorIntoIterator<T, +Iterator<T>> of IntoIterator<T> {
self
}
}

impl SnapshotFixedSizeArrayIntoIterator<
T, const SIZE: usize, +Drop<T>, impl ToSpan: core::array::ToSpanTrait<[T; SIZE], T>,
> of IntoIterator<@[T; SIZE]> {
type IntoIter = crate::array::SpanIter<T>;
fn into_iter(self: @[T; SIZE]) -> Self::IntoIter {
ToSpan::span(self).into_iter()
}
}
Loading

0 comments on commit 82d27d2

Please sign in to comment.