Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TotalOrd library to core::ops for uint types #6635

Merged
merged 16 commits into from
Oct 23, 2024
3 changes: 2 additions & 1 deletion forc-plugins/forc-doc/src/tests/expects/impl_trait/mod.rs

Large diffs are not rendered by default.

116 changes: 116 additions & 0 deletions sway-lib-core/src/ops.sw
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,122 @@ impl Shift for u16 {
}
}

/// Trait to compare values of the same type.
pub trait TotalOrd {
sdankel marked this conversation as resolved.
Show resolved Hide resolved
/// 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;
}

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

fn max(self, other: Self) -> Self {
if self > other { self } else { other }
}
}

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

fn max(self, other: Self) -> Self {
if self > other { self } else { other }
}
}

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

fn max(self, other: Self) -> Self {
if self > other { self } else { other }
}
}

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

fn max(self, other: Self) -> Self {
if self > other { self } else { other }
}
}

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

fn max(self, other: Self) -> Self {
if self > other { self } else { other }
}
}

impl Shift for u8 {
fn lsh(self, other: u64) -> Self {
__and(__lsh(self, other), Self::max())
Expand Down
10 changes: 10 additions & 0 deletions sway-lib-std/src/u128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -864,3 +864,13 @@ impl Logarithm for U128 {
result
}
}

impl core::ops::TotalOrd for U128 {
fn min(self, other: Self) -> Self {
if self < other { self } else { other }
}

fn max(self, other: Self) -> Self {
if self > other { self } else { other }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-AA6BA29B6C5ED809"

[[package]]
name = "std"
source = "path+from-root-AA6BA29B6C5ED809"
dependencies = ["core"]

[[package]]
name = "totalord"
source = "member"
dependencies = ["std"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "totalord"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"concreteTypes": [
{
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
"type": "bool"
},
{
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
"type": "u16"
},
{
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
"type": "u256"
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"type": "u32"
},
{
"concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0",
"type": "u64"
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"type": "u8"
}
],
"configurables": [],
"encodingVersion": "1",
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903"
}
],
"loggedTypes": [
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"logId": "14454674236531057292"
},
{
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
"logId": "2992671284987479467"
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"logId": "15520703124961489725"
},
{
"concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0",
"logId": "1515152261580153489"
},
{
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
"logId": "1970142151624111756"
}
],
"messagesTypes": [],
"metadataTypes": [],
"programType": "script",
"specVersion": "1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"concreteTypes": [
{
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
"type": "bool"
},
{
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
"type": "u16"
},
{
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
"type": "u256"
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"type": "u32"
},
{
"concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0",
"type": "u64"
},
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"type": "u8"
}
],
"configurables": [],
"encodingVersion": "1",
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903"
}
],
"loggedTypes": [
{
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"logId": "14454674236531057292"
},
{
"concreteTypeId": "29881aad8730c5ab11d275376323d8e4ff4179aae8ccb6c13fe4902137e162ef",
"logId": "2992671284987479467"
},
{
"concreteTypeId": "d7649d428b9ff33d188ecbf38a7e4d8fd167fa01b2e10fe9a8f9308e52f1d7cc",
"logId": "15520703124961489725"
},
{
"concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0",
"logId": "1515152261580153489"
},
{
"concreteTypeId": "1b5759d94094368cfd443019e7ca5ec4074300e544e5ea993a979f5da627261e",
"logId": "1970142151624111756"
}
],
"messagesTypes": [],
"metadataTypes": [],
"programType": "script",
"specVersion": "1"
}
Loading
Loading