From c9afe4dc4047a4582af2f853eb23feb7fd49b90f Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 26 Jun 2024 08:52:24 +0800 Subject: [PATCH 1/3] unreachable to ord for amount --- src/interface/contract.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/interface/contract.rs b/src/interface/contract.rs index a02562ff..7878c2bd 100644 --- a/src/interface/contract.rs +++ b/src/interface/contract.rs @@ -19,6 +19,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::cmp::Ordering; use std::collections::HashMap; use amplify::confinement::{SmallOrdSet, SmallVec}; @@ -154,10 +155,11 @@ impl StateChange for AmountChange { *self = match self { AmountChange::Dec(neg) => AmountChange::Dec(*neg + sub), AmountChange::Zero => AmountChange::Dec(sub), - AmountChange::Inc(pos) if *pos > sub => AmountChange::Inc(*pos - sub), - AmountChange::Inc(pos) if *pos == sub => AmountChange::Zero, - AmountChange::Inc(pos) if *pos < sub => AmountChange::Dec(sub - *pos), - AmountChange::Inc(_) => unreachable!(), + AmountChange::Inc(pos) => match sub.cmp(pos) { + Ordering::Less => AmountChange::Inc(*pos - sub), + Ordering::Equal => AmountChange::Zero, + Ordering::Greater => AmountChange::Dec(sub - *pos), + }, }; } @@ -165,10 +167,11 @@ impl StateChange for AmountChange { *self = match self { AmountChange::Inc(pos) => AmountChange::Inc(*pos + add), AmountChange::Zero => AmountChange::Inc(add), - AmountChange::Dec(neg) if *neg > add => AmountChange::Dec(*neg - add), - AmountChange::Dec(neg) if *neg == add => AmountChange::Zero, - AmountChange::Dec(neg) if *neg < add => AmountChange::Inc(add - *neg), - AmountChange::Dec(_) => unreachable!(), + AmountChange::Dec(neg) => match add.cmp(neg) { + Ordering::Greater => AmountChange::Dec(*neg - add), + Ordering::Equal => AmountChange::Zero, + Ordering::Less => AmountChange::Inc(add - *neg), + }, }; } } From 50667a40f2013b0fbcc5d340cab01bd495a0df65 Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 26 Jun 2024 08:52:24 +0800 Subject: [PATCH 2/3] unreachable to ord for amount --- src/interface/contract.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/interface/contract.rs b/src/interface/contract.rs index a02562ff..7878c2bd 100644 --- a/src/interface/contract.rs +++ b/src/interface/contract.rs @@ -19,6 +19,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::cmp::Ordering; use std::collections::HashMap; use amplify::confinement::{SmallOrdSet, SmallVec}; @@ -154,10 +155,11 @@ impl StateChange for AmountChange { *self = match self { AmountChange::Dec(neg) => AmountChange::Dec(*neg + sub), AmountChange::Zero => AmountChange::Dec(sub), - AmountChange::Inc(pos) if *pos > sub => AmountChange::Inc(*pos - sub), - AmountChange::Inc(pos) if *pos == sub => AmountChange::Zero, - AmountChange::Inc(pos) if *pos < sub => AmountChange::Dec(sub - *pos), - AmountChange::Inc(_) => unreachable!(), + AmountChange::Inc(pos) => match sub.cmp(pos) { + Ordering::Less => AmountChange::Inc(*pos - sub), + Ordering::Equal => AmountChange::Zero, + Ordering::Greater => AmountChange::Dec(sub - *pos), + }, }; } @@ -165,10 +167,11 @@ impl StateChange for AmountChange { *self = match self { AmountChange::Inc(pos) => AmountChange::Inc(*pos + add), AmountChange::Zero => AmountChange::Inc(add), - AmountChange::Dec(neg) if *neg > add => AmountChange::Dec(*neg - add), - AmountChange::Dec(neg) if *neg == add => AmountChange::Zero, - AmountChange::Dec(neg) if *neg < add => AmountChange::Inc(add - *neg), - AmountChange::Dec(_) => unreachable!(), + AmountChange::Dec(neg) => match add.cmp(neg) { + Ordering::Greater => AmountChange::Dec(*neg - add), + Ordering::Equal => AmountChange::Zero, + Ordering::Less => AmountChange::Inc(add - *neg), + }, }; } } From 708b2070587308de43cc685bb3166c98b7818555 Mon Sep 17 00:00:00 2001 From: jack Date: Thu, 27 Jun 2024 21:40:43 +0800 Subject: [PATCH 3/3] fix less --- src/interface/contract.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interface/contract.rs b/src/interface/contract.rs index 7878c2bd..5a76bca2 100644 --- a/src/interface/contract.rs +++ b/src/interface/contract.rs @@ -168,9 +168,9 @@ impl StateChange for AmountChange { AmountChange::Inc(pos) => AmountChange::Inc(*pos + add), AmountChange::Zero => AmountChange::Inc(add), AmountChange::Dec(neg) => match add.cmp(neg) { - Ordering::Greater => AmountChange::Dec(*neg - add), + Ordering::Less => AmountChange::Dec(*neg - add), Ordering::Equal => AmountChange::Zero, - Ordering::Less => AmountChange::Inc(add - *neg), + Ordering::Greater => AmountChange::Inc(add - *neg), }, }; }