Skip to content

Commit

Permalink
Using Ok,Err,Some,None from prelude.
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi committed Jan 25, 2025
1 parent 4022ede commit a3255a8
Show file tree
Hide file tree
Showing 123 changed files with 1,462 additions and 1,548 deletions.
92 changes: 45 additions & 47 deletions corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//!
//! ```
//! let mut arr = array![1, 2];
//! let one = arr.pop_front(); // Returns Option::Some(1)
//! let one = arr.pop_front(); // Returns Some(1)
//! ```
//!
//! Arrays support indexing (through the [`IndexView`] trait):
Expand Down Expand Up @@ -145,57 +145,57 @@ pub impl ArrayImpl<T> of ArrayTrait<T> {
/// ```
fn append_span<+Clone<T>, +Drop<T>>(ref self: Array<T>, mut span: Span<T>) {
match span.pop_front() {
Option::Some(current) => {
Some(current) => {
self.append(current.clone());
self.append_span(span);
},
Option::None => {},
None => {},
};
}

/// Pops a value from the front of the array.
/// Returns `Option::Some(value)` if the array is not empty, `Option::None` otherwise.
/// Returns `Some(value)` if the array is not empty, `None` otherwise.
///
/// # Examples
///
/// ```
/// let mut arr = array![2, 3, 4];
/// assert!(arr.pop_front() == Option::Some(2));
/// assert!(arr.pop_front() == Option::Some(3));
/// assert!(arr.pop_front() == Option::Some(4));
/// assert!(arr.pop_front() == Some(2));
/// assert!(arr.pop_front() == Some(3));
/// assert!(arr.pop_front() == Some(4));
/// assert!(arr.pop_front().is_none());
/// ```
#[inline]
fn pop_front(ref self: Array<T>) -> Option<T> nopanic {
match array_pop_front(ref self) {
Option::Some(x) => Option::Some(x.unbox()),
Option::None => Option::None,
Some(x) => Some(x.unbox()),
None => None,
}
}

/// Pops a value from the front of the array.
/// Returns an option containing the remaining array and the value removed if the array is
/// not empty, otherwise `Option::None` and drops the array.
/// not empty, otherwise `None` and drops the array.
///
/// # Examples
///
/// ```
/// let arr = array![2, 3, 4];
/// assert!(arr.pop_front_consume() == Option::Some((array![3, 4], 2)));
/// assert!(arr.pop_front_consume() == Some((array![3, 4], 2)));
///
/// let arr: Array<u8> = array![];
/// assert!(arr.pop_front_consume().is_none());
/// ```
#[inline]
fn pop_front_consume(self: Array<T>) -> Option<(Array<T>, T)> nopanic {
match array_pop_front_consume(self) {
Option::Some((arr, x)) => Option::Some((arr, x.unbox())),
Option::None => Option::None,
Some((arr, x)) => Some((arr, x.unbox())),
None => None,
}
}

/// Returns an option containing a box of a snapshot of the element at the given 'index'
/// if the array contains this index, 'Option::None' otherwise.
/// if the array contains this index, 'None' otherwise.
///
/// Element at index 0 is the front of the array.
///
Expand Down Expand Up @@ -257,8 +257,8 @@ pub impl ArrayImpl<T> of ArrayTrait<T> {
fn is_empty(self: @Array<T>) -> bool {
let mut snapshot = self;
match array_snapshot_pop_front(ref snapshot) {
Option::Some(_) => false,
Option::None => true,
Some(_) => false,
None => true,
}
}

Expand Down Expand Up @@ -334,19 +334,19 @@ impl ArraySerde<T, +Serde<T>, +Drop<T>> of Serde<Array<T>> {

fn serialize_array_helper<T, +Serde<T>, +Drop<T>>(mut input: Span<T>, ref output: Array<felt252>) {
match input.pop_front() {
Option::Some(value) => {
Some(value) => {
value.serialize(ref output);
serialize_array_helper(input, ref output);
},
Option::None => {},
None => {},
}
}

fn deserialize_array_helper<T, +Serde<T>, +Drop<T>>(
ref serialized: Span<felt252>, mut curr_output: Array<T>, remaining: felt252,
) -> Option<Array<T>> {
if remaining == 0 {
return Option::Some(curr_output);
return Some(curr_output);
}
curr_output.append(Serde::deserialize(ref serialized)?);
deserialize_array_helper(ref serialized, curr_output, remaining - 1)
Expand Down Expand Up @@ -425,7 +425,7 @@ impl SpanFelt252Serde of Serde<Span<felt252>> {
let length: u32 = (*serialized.pop_front()?).try_into()?;
let res = serialized.slice(0, length);
serialized = serialized.slice(length, serialized.len() - length);
Option::Some(res)
Some(res)
}
}

Expand Down Expand Up @@ -457,55 +457,55 @@ impl SpanSerde<T, +Serde<T>, +Drop<T>, -TypeEqual<felt252, T>> of Serde<Span<T>>
fn deserialize(ref serialized: Span<felt252>) -> Option<Span<T>> {
let length = *serialized.pop_front()?;
let mut arr = array_new();
Option::Some(deserialize_array_helper(ref serialized, arr, length)?.span())
Some(deserialize_array_helper(ref serialized, arr, length)?.span())
}
}

/// Basic trait for the `Span` type.
#[generate_trait]
pub impl SpanImpl<T> of SpanTrait<T> {
/// Pops a value from the front of the span.
/// Returns `Option::Some(@value)` if the array is not empty, `Option::None` otherwise.
/// Returns `Some(@value)` if the array is not empty, `None` otherwise.
///
/// # Examples
///
/// ```
/// let mut span = array![1, 2, 3].span();
/// assert!(span.pop_front() == Option::Some(@1));
/// assert!(span.pop_front() == Some(@1));
/// ```
#[inline]
fn pop_front(ref self: Span<T>) -> Option<@T> {
let mut snapshot = self.snapshot;
let item = array_snapshot_pop_front(ref snapshot);
self = Span { snapshot };
match item {
Option::Some(x) => Option::Some(x.unbox()),
Option::None => Option::None,
Some(x) => Some(x.unbox()),
None => None,
}
}

/// Pops a value from the back of the span.
/// Returns `Option::Some(@value)` if the array is not empty, `Option::None` otherwise.
/// Returns `Some(@value)` if the array is not empty, `None` otherwise.
///
/// # Examples
/// ```
/// let mut span = array![1, 2, 3].span();
/// assert!(span.pop_back() == Option::Some(@3));
/// assert!(span.pop_back() == Some(@3));
/// ```
#[inline]
fn pop_back(ref self: Span<T>) -> Option<@T> {
let mut snapshot = self.snapshot;
let item = array_snapshot_pop_back(ref snapshot);
self = Span { snapshot };
match item {
Option::Some(x) => Option::Some(x.unbox()),
Option::None => Option::None,
Some(x) => Some(x.unbox()),
None => None,
}
}

/// Pops multiple values from the front of the span.
/// Returns an option containing a snapshot of a box that contains the values as a fixed-size
/// array if the action completed successfully, 'Option::None' otherwise.
/// array if the action completed successfully, 'None' otherwise.
///
/// # Examples
///
Expand All @@ -521,7 +521,7 @@ pub impl SpanImpl<T> of SpanTrait<T> {

/// Pops multiple values from the back of the span.
/// Returns an option containing a snapshot of a box that contains the values as a fixed-size
/// array if the action completed successfully, 'Option::None' otherwise.
/// array if the action completed successfully, 'None' otherwise.
///
/// # Examples
///
Expand All @@ -536,7 +536,7 @@ pub impl SpanImpl<T> of SpanTrait<T> {
}

/// Returns an option containing a box of a snapshot of the element at the given 'index'
/// if the span contains this index, 'Option::None' otherwise.
/// if the span contains this index, 'None' otherwise.
///
/// Element at index 0 is the front of the array.
///
Expand Down Expand Up @@ -613,8 +613,8 @@ pub impl SpanImpl<T> of SpanTrait<T> {
fn is_empty(self: Span<T>) -> bool {
let mut snapshot = self.snapshot;
match array_snapshot_pop_front(ref snapshot) {
Option::Some(_) => false,
Option::None => true,
Some(_) => false,
None => true,
}
}
}
Expand Down Expand Up @@ -732,7 +732,7 @@ impl SpanTryIntoFixedSizedArray<

impl SpanTryIntoEmptyFixedSizedArray<T, +Drop<T>> of TryInto<Span<T>, @Box<[T; 0]>> {
/// Returns an option of a snapshot of a box that contains an empty fixed-size array if the span
/// is empty, and `Option::None` otherwise.
/// is empty, and `None` otherwise.
///
/// # Examples
///
Expand All @@ -743,9 +743,9 @@ impl SpanTryIntoEmptyFixedSizedArray<T, +Drop<T>> of TryInto<Span<T>, @Box<[T; 0
#[inline]
fn try_into(self: Span<T>) -> Option<@Box<[T; 0]>> {
if self.is_empty() {
Option::Some(@BoxTrait::new([]))
Some(@BoxTrait::new([]))
} else {
Option::None
None
}
}
}
Expand All @@ -757,8 +757,8 @@ impl ArrayTCloneImpl<T, +Clone<T>, +Drop<T>> of Clone<Array<T>> {
let mut span = self.span();
loop {
match span.pop_front() {
Option::Some(v) => { response.append(v.clone()); },
Option::None => { break (); },
Some(v) => { response.append(v.clone()); },
None => { break (); },
};
};
response
Expand All @@ -780,12 +780,10 @@ impl SpanPartialEq<T, +PartialEq<T>> of PartialEq<Span<T>> {
let mut rhs_span = *rhs;
loop {
match lhs_span.pop_front() {
Option::Some(lhs_v) => {
if lhs_v != rhs_span.pop_front().unwrap() {
break false;
}
},
Option::None => { break true; },
Some(lhs_v) => { if lhs_v != rhs_span.pop_front().unwrap() {
break false;
} },
None => { break true; },
};
}
}
Expand All @@ -802,7 +800,7 @@ impl SpanIterCopy<T> of Copy<SpanIter<T>>;
impl SpanIterator<T> of Iterator<SpanIter<T>> {
/// The type of the elements being iterated over.
type Item = @T;
/// Advances the iterator and returns the next value. Returns `Option::None` when iteration is
/// Advances the iterator and returns the next value. Returns `None` when iteration is
/// finished.
fn next(ref self: SpanIter<T>) -> Option<@T> {
self.span.pop_front()
Expand Down Expand Up @@ -838,7 +836,7 @@ impl ArrayIterClone<T, +crate::clone::Clone<T>, +Drop<T>> of crate::clone::Clone
impl ArrayIterator<T> of Iterator<ArrayIter<T>> {
/// The type of the elements being iterated over.
type Item = T;
/// Advances the iterator and returns the next value. Returns `Option::None` when iteration is
/// Advances the iterator and returns the next value. Returns `None` when iteration is
/// finished.
fn next(ref self: ArrayIter<T>) -> Option<T> {
self.array.pop_front()
Expand Down
14 changes: 7 additions & 7 deletions corelib/src/boolean.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@
//!
//! let bool_value = true;
//! let result = bool_value.then_some(42_u8);
//! assert!(result == Option::Some(42));
//! assert!(result == Some(42));
//!
//! let bool_value = false;
//! let result = bool_value.then_some(42_u8);
//! assert!(result == Option::None);
//! assert!(result == None);
//! ```

/// Basic trait for boolean operations.
/// Explicit import of `BoolTrait` is required with `use core::boolean::BoolTrait;`.
#[generate_trait]
pub impl BoolImpl<T, +Drop<T>> of BoolTrait<T> {
/// Returns `Option::Some(t)` if the `bool` is `true`, `Option::None` otherwise.
/// Returns `Some(t)` if the `bool` is `true`, `None` otherwise.
///
/// # Examples
///
/// ```
/// assert!(false.then_some(0) == Option::None);
/// assert!(true.then_some(0) == Option::Some(0));
/// assert!(false.then_some(0) == None);
/// assert!(true.then_some(0) == Some(0));
/// ```
#[inline]
fn then_some(self: bool, t: T) -> Option<T> nopanic {
if self {
Option::Some(t)
Some(t)
} else {
Option::None
None
}
}
}
Loading

0 comments on commit a3255a8

Please sign in to comment.