Skip to content

Commit

Permalink
Remove a duplicate compare and a panic from compare.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Sep 7, 2023
1 parent 2b7d6ea commit 6a71c37
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
20 changes: 13 additions & 7 deletions soroban-env-common/src/compare.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#[cfg(feature = "std")]
use std::rc::Rc;

use crate::{val::ValConvert, Env, Tag, Val};
use crate::{
val::ValConvert,
xdr::{ScErrorCode, ScErrorType},
Env, Error, Tag, Val,
};
use core::cmp::Ordering;

/// General trait representing the ability to compare two values of some type.
Expand Down Expand Up @@ -51,11 +55,9 @@ macro_rules! impl_compare_for_tuple {

fn compare(&self, a: &($($T,)+), b: &($($T,)+)) -> Result<Ordering, Self::Error> {
$(
if let Ordering::Less = <C as Compare<$T>>::compare(self, &a.$idx, &b.$idx)? {
return Ok(Ordering::Less)
}
if let Ordering::Greater = <C as Compare<$T>>::compare(self, &a.$idx, &b.$idx)? {
return Ok(Ordering::Greater)
match <C as Compare<$T>>::compare(self, &a.$idx, &b.$idx)? {
unequal @ (Ordering::Less | Ordering::Greater) => return Ok(unequal),
_ => ()
}
)*
Ok(Ordering::Equal)
Expand Down Expand Up @@ -194,7 +196,11 @@ impl<E: Env> Compare<Val> for E {
| Tag::SymbolObject
| Tag::VecObject
| Tag::MapObject
| Tag::AddressObject => unreachable!(),
| Tag::AddressObject => Err(Error::from_type_and_code(
ScErrorType::Context,
ScErrorCode::InternalError,
)
.into()),

Tag::ObjectCodeUpperBound => Ok(Ordering::Equal),
Tag::Bad => Ok(Ordering::Equal),
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait EnvBase: Sized + Clone {
/// environment-interface level, and then either directly handle or escalate
/// the contained `Error` code to the user as a `Error` or `Result<>` of
/// some other type, depending on the API.
type Error: core::fmt::Debug + Into<crate::Error>;
type Error: core::fmt::Debug + Into<crate::Error> + From<crate::Error>;

/// Reject an error from the environment, turning it into a panic but on
/// terms that the environment controls (eg. transforming or logging it).
Expand Down
6 changes: 6 additions & 0 deletions soroban-env-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ impl From<core::convert::Infallible> for crate::Error {
}
}

impl From<crate::Error> for core::convert::Infallible {
fn from(_value: crate::Error) -> Self {
unreachable!()
}
}

#[cfg(all(test, feature = "std"))]
mod tests {
use super::*;
Expand Down

0 comments on commit 6a71c37

Please sign in to comment.