Skip to content

Commit

Permalink
feat(corelib): range is_empty (#6925)
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 authored Dec 26, 2024
1 parent 20caf9b commit d6f6e5d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion corelib/src/ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ mod deref;
pub use deref::DerefMut;
pub use deref::{Deref, SnapshotDeref};
mod range;
pub use range::Range;
// `RangeOp` is used internally by the compiler.
#[allow(unused_imports)]
use range::RangeOp;
pub use range::{Range, RangeTrait};

mod function;
pub use function::{Fn, FnOnce};
17 changes: 17 additions & 0 deletions corelib/src/ops/range.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ pub struct Range<T> {
pub end: T,
}

#[generate_trait]
pub impl RangeImpl<T, +Copy<T>, +Drop<T>, +PartialOrd<T>> of RangeTrait<T> {
/// Returns `true` if the range contains no items.
///
/// # Examples
///
/// ```
/// assert!(!(3_u8..5_u8).is_empty());
/// assert!( (3_u8..3_u8).is_empty());
/// assert!( (3_u8..2_u8).is_empty());
/// ```
#[inline]
fn is_empty(self: @Range<T>) -> bool {
!(self.start < self.end)
}
}

impl RangeDebug<T, impl TDebug: crate::fmt::Debug<T>> of crate::fmt::Debug<Range<T>> {
/// Formats a `Range` type, allowing to print `Range` instances for debugging purposes.
///
Expand Down
9 changes: 9 additions & 0 deletions corelib/src/test/range_test.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
use core::ops::RangeTrait;

#[test]
fn test_range_is_empty() {
assert!(!(3_u8..5_u8).is_empty());
assert!((3_u8..3_u8).is_empty());
assert!((3_u8..2_u8).is_empty());
}

#[test]
fn test_range_format() {
assert!(format!("{:?}", 1..5) == "1..5");
Expand Down

0 comments on commit d6f6e5d

Please sign in to comment.