Trait core::ops::TotalOrd

pub trait TotalOrd {
    /// Finds the minimum value of two values of the same type.
    ///
    /// # Arguments
    ///
    /// * `other`: [Self] - The value of the same type.
    ///
    /// # Returns
    ///
    /// * Self - the minimum of the two values, or the same value if they are equal.
    ///
    /// # Examples
    ///
    /// ```sway
    /// struct MyStruct {
    ///     val: u64,
    /// }
    ///
    /// impl TotalOrd for MyStruct {
    ///     fn min(self, other: Self) -> Self {
    ///         if self.val < other.val { self } else { other }
    ///     }
    /// }
    ///
    /// fn foo() {
    ///     let struct1 = MyStruct { val: 10 };
    ///     let struct2 = MyStruct { val: 20 };
    ///     let min = struct1.min(struct2);
    ///     assert(min.val == struct1.val);
    /// }
    /// ```
    fn min(self, other: Self) -> Self;
    /// Finds the maximum value of two values of the same type.
    ///
    /// # Arguments
    ///
    /// * `other`: [Self] - The value of the same type.
    ///
    /// # Returns
    ///
    /// * Self - the maximum of the two values, or the same value if they are equal.
    ///
    /// # Examples
    ///
    /// ```sway
    /// struct MyStruct {
    ///     val: u64,
    /// }
    ///
    /// impl TotalOrd for MyStruct {
    ///     fn max(self, other: Self) -> Self {
    ///         if self.val > other.val { self } else { other }
    ///     }
    /// }
    ///
    /// fn foo() {
    ///     let struct1 = MyStruct { val: 10 };
    ///     let struct2 = MyStruct { val: 20 };
    ///     let max = struct1.max(struct2);
    ///     assert(max.val == struct2.val);
    /// }
    /// ```
    fn max(self, other: Self) -> Self;
}
Expand description

Trait to compare values of the same type.

Required Methods

Finds the minimum value of two values of the same type.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • Self - the minimum of the two values, or the same value if they are equal.

Examples

struct MyStruct {
    val: u64,
}

impl TotalOrd for MyStruct {
    fn min(self, other: Self) -> Self {
        if self.val < other.val { self } else { other }
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 20 };
    let min = struct1.min(struct2);
    assert(min.val == struct1.val);
}

Finds the maximum value of two values of the same type.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • Self - the maximum of the two values, or the same value if they are equal.

Examples

struct MyStruct {
    val: u64,
}

impl TotalOrd for MyStruct {
    fn max(self, other: Self) -> Self {
        if self.val > other.val { self } else { other }
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 20 };
    let max = struct1.max(struct2);
    assert(max.val == struct2.val);
}