From 2dc18c2c35aa8a4889d959bb67edd192ff97eb12 Mon Sep 17 00:00:00 2001 From: Tristan <122918260+TAdev0@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:15:32 +0100 Subject: [PATCH] Core Lib Documentation: `Zeroable` module (#6722) Co-authored-by: enitrat --- corelib/src/zeroable.cairo | 42 +++++++++++++++----------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/corelib/src/zeroable.cairo b/corelib/src/zeroable.cairo index 4a1a0fe6195..a48df1a0825 100644 --- a/corelib/src/zeroable.cairo +++ b/corelib/src/zeroable.cairo @@ -1,23 +1,30 @@ -// === Zeroable === +//! Types and traits for handling non-zero values and zero checking operations. +//! +//! This module provides the [`NonZero`] wrapper type which guarantees that a value is never +//! zero. +//! The [`Zeroable`] trait is meant for internal use only. The public-facing equivalent is the +//! [`Zero`] trait. +//! +//! [`Zero`]: core::num::traits::zero::Zero /// A trait for types that have a concept of zero and can be compared to zero. /// /// This trait is useful for numeric types or any type that has an additive identity element. pub(crate) trait Zeroable { - /// Returns the additive identity element of Self, 0. + /// Returns the additive identity element of `self`, 0. /// - /// This method should return a value that, when added to any other value of type T, + /// This method should return a value that, when added to any other value of type `T`, /// does not change that value. /// /// # Examples /// /// ``` - /// assert_eq!(Zeroable::::zero(), 0); + /// assert!(Zeroable::::zero() == 0); /// ``` #[must_use] fn zero() -> T; - /// Returns whether self is equal to 0, the additive identity element. + /// Returns whether `self` is equal to 0, the additive identity element. /// /// # Examples /// @@ -28,7 +35,7 @@ pub(crate) trait Zeroable { #[must_use] fn is_zero(self: T) -> bool; - /// Returns whether self is not equal to 0, the additive identity element. + /// Returns whether `self` is not equal to 0, the additive identity element. /// /// This method is the logical inverse of `is_zero()`. /// @@ -48,18 +55,15 @@ pub(crate) mod zero_based { pub(crate) impl ZeroableImpl< T, impl ZeroImpl: crate::num::traits::Zero, +Drop, +Copy, > of super::Zeroable { - /// Returns the zero value for the type. fn zero() -> T { ZeroImpl::zero() } - /// Checks if the value is zero. #[inline] fn is_zero(self: T) -> bool { ZeroImpl::is_zero(@self) } - /// Checks if the value is non-zero. #[inline] fn is_non_zero(self: T) -> bool { ZeroImpl::is_non_zero(@self) @@ -69,13 +73,13 @@ pub(crate) mod zero_based { pub(crate) impl Felt252Zeroable = zero_based::ZeroableImpl; -// === NonZero === /// A wrapper type for non-zero values of type T. /// /// This type guarantees that the wrapped value is never zero. #[derive(Copy, Drop)] pub extern type NonZero; + impl NonZeroNeg, +TryInto>> of Neg> { fn neg(a: NonZero) -> NonZero { // TODO(orizi): Optimize using bounded integers. @@ -89,26 +93,20 @@ impl NonZeroNeg, +TryInto>> of Neg> { pub(crate) enum IsZeroResult { /// Indicates that the value is zero. Zero, - /// Indicates that the value is non-zero, wrapping it in a NonZero. + /// Indicates that the value is non-zero, wrapping it in a `NonZero`. NonZero: NonZero, } -/// Unwraps a NonZero to retrieve the underlying value of type T. +/// Unwraps a `NonZero` to retrieve the underlying value of type `T`. extern fn unwrap_non_zero(a: NonZero) -> T nopanic; -/// Implements the `Into` trait for converting NonZero to T. pub(crate) impl NonZeroIntoImpl of Into, T> { - /// Converts a NonZero to T. fn into(self: NonZero) -> T nopanic { unwrap_non_zero(self) } } -/// Implements the `Into` trait for converting IsZeroResult to bool. impl IsZeroResultIntoBool> of Into, bool> { - /// Converts an IsZeroResult to a boolean. - /// - /// Returns true if the result is Zero, false otherwise. fn into(self: IsZeroResult) -> bool { match self { IsZeroResult::Zero => true, @@ -117,9 +115,7 @@ impl IsZeroResultIntoBool> of Into, bool> { } } -/// Implements the `PartialEq` trait for NonZero. impl NonZeroPartialEq, +Copy, +Drop> of PartialEq> { - /// Checks if two NonZero values are equal. #[inline] fn eq(lhs: @NonZero, rhs: @NonZero) -> bool { let lhs: T = (*lhs).into(); @@ -127,7 +123,6 @@ impl NonZeroPartialEq, +Copy, +Drop> of PartialEq values are not equal. #[inline] fn ne(lhs: @NonZero, rhs: @NonZero) -> bool { let lhs: T = (*lhs).into(); @@ -136,17 +131,12 @@ impl NonZeroPartialEq, +Copy, +Drop> of PartialEq. impl NonZeroSerde, +Copy, +Drop, +TryInto>> of Serde> { - /// Serializes a NonZero value. fn serialize(self: @NonZero, ref output: Array) { let value: T = (*self).into(); value.serialize(ref output); } - /// Deserializes a NonZero value. - /// - /// Returns None if deserialization fails or if the deserialized value is zero. fn deserialize(ref serialized: Span) -> Option> { Serde::::deserialize(ref serialized)?.try_into() }