soroban_env_common/
compare.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#[cfg(feature = "std")]
use std::rc::Rc;

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.
/// Similar to `core::cmp::Cmp` but with two key differences: the comparison is
/// fallible, and is provided by some external type implementing `Compare`
/// rather than the compared type itself.
///
/// This trait exists to support comparing `Val`s with help from the
/// environment in which object handles are defined, and allowing for the
/// possibility of erroneous inputs like invalid object handles, as well as the
/// possibility of running over budget during the comparison. It is also used
/// in other places where comparison work has to be budgeted, such as inside
/// the host's storage maps.
pub trait Compare<T> {
    type Error;
    fn compare(&self, a: &T, b: &T) -> Result<Ordering, Self::Error>;
}

impl<T, C: Compare<T>> Compare<&T> for C {
    type Error = C::Error;

    fn compare(&self, a: &&T, b: &&T) -> Result<Ordering, Self::Error> {
        <C as Compare<T>>::compare(self, *a, *b)
    }
}

impl<T, C: Compare<T>> Compare<Option<T>> for C {
    type Error = C::Error;

    fn compare(&self, a: &Option<T>, b: &Option<T>) -> Result<Ordering, Self::Error> {
        match (a, b) {
            (Some(a), Some(b)) => <C as Compare<T>>::compare(self, a, b),
            (None, None) => Ok(Ordering::Equal),
            (None, Some(_)) => Ok(Ordering::Less),
            (Some(_), None) => Ok(Ordering::Greater),
        }
    }
}

macro_rules! impl_compare_for_tuple {
    ( $($idx:tt $T:ident),+) => {
        impl<$($T,)+ E, C> Compare<($($T,)+)> for C
        where
            $(C: Compare<$T, Error = E>,)+
        {
            type Error = E;

            fn compare(&self, a: &($($T,)+), b: &($($T,)+)) -> Result<Ordering, Self::Error> {
                $(
                    match <C as Compare<$T>>::compare(self, &a.$idx, &b.$idx)? {
                        unequal @ (Ordering::Less | Ordering::Greater) => return Ok(unequal),
                        _ => ()
                    }
                )*
                Ok(Ordering::Equal)
            }
        }
    };
}

impl_compare_for_tuple!(0 T, 1 U);
impl_compare_for_tuple!(0 T, 1 U, 2 V);
impl_compare_for_tuple!(0 T, 1 U, 2 V, 3 W);
impl_compare_for_tuple!(0 T, 1 U, 2 V, 3 W, 4 X);

#[cfg(feature = "std")]
impl<T, C: Compare<T>> Compare<Vec<T>> for C {
    type Error = C::Error;

    fn compare(&self, a: &Vec<T>, b: &Vec<T>) -> Result<Ordering, Self::Error> {
        let mut i = 0;
        loop {
            match (a.get(i), b.get(i)) {
                (None, None) => return Ok(Ordering::Equal),
                (None, Some(_)) => return Ok(Ordering::Less),
                (Some(_), None) => return Ok(Ordering::Greater),
                (Some(a), Some(b)) => match <C as Compare<T>>::compare(self, a, b)? {
                    Ordering::Equal => i += 1,
                    unequal => return Ok(unequal),
                },
            }
        }
    }
}

#[cfg(feature = "std")]
impl<T, C: Compare<T>> Compare<Box<T>> for C {
    type Error = C::Error;

    fn compare(&self, a: &Box<T>, b: &Box<T>) -> Result<Ordering, Self::Error> {
        <Self as Compare<T>>::compare(self, a, b)
    }
}

#[cfg(feature = "std")]
impl<T, C: Compare<T>> Compare<Rc<T>> for C {
    type Error = C::Error;

    fn compare(&self, a: &Rc<T>, b: &Rc<T>) -> Result<Ordering, Self::Error> {
        <Self as Compare<T>>::compare(self, a, b)
    }
}

// Apparently we can't do a blanket T:Ord impl because there are Ord derivations
// that also go through &T and Option<T> that conflict with our impls above
// (patches welcome from someone who understands trait-system workarounds
// better). But we can list out any concrete Ord instances we want to support
// here.

macro_rules! delegate_compare_to_wrapper {
    ($T:ident,$A:ident,$B:ident,$SELF:ident) => {{
        let a = unsafe { crate::$T::unchecked_from_val(*$A) };
        let b = unsafe { crate::$T::unchecked_from_val(*$B) };
        $SELF.compare(&a, &b)
    }};
}

#[allow(clippy::comparison_chain)]
impl<E: Env> Compare<Val> for E {
    type Error = E::Error;

    fn compare(&self, a: &Val, b: &Val) -> Result<Ordering, Self::Error> {
        if a.get_payload() == b.get_payload() {
            // Fast-path exactly-equal values.
            return Ok(Ordering::Equal);
        }
        if a.is_object() || b.is_object() {
            // Delegate any object-comparing to environment.
            let v = self.obj_cmp(*a, *b)?;
            return if v == 0 {
                Ok(Ordering::Equal)
            } else if v < 0 {
                Ok(Ordering::Less)
            } else {
                Ok(Ordering::Greater)
            };
        }
        let a_tag = a.get_tag();
        let b_tag = b.get_tag();
        if a_tag < b_tag {
            Ok(Ordering::Less)
        } else if a_tag > b_tag {
            Ok(Ordering::Greater)
        } else {
            // Tags are equal so we only have to switch on one.
            match a_tag {
                Tag::False => Ok(Ordering::Equal),
                Tag::True => Ok(Ordering::Equal),
                Tag::Void => Ok(Ordering::Equal),

                Tag::Error => delegate_compare_to_wrapper!(Error, a, b, self),

                Tag::U32Val => delegate_compare_to_wrapper!(U32Val, a, b, self),
                Tag::I32Val => delegate_compare_to_wrapper!(I32Val, a, b, self),

                Tag::U64Small => delegate_compare_to_wrapper!(U64Small, a, b, self),
                Tag::I64Small => delegate_compare_to_wrapper!(I64Small, a, b, self),

                Tag::TimepointSmall => delegate_compare_to_wrapper!(TimepointSmall, a, b, self),
                Tag::DurationSmall => delegate_compare_to_wrapper!(DurationSmall, a, b, self),

                Tag::U128Small => delegate_compare_to_wrapper!(U128Small, a, b, self),
                Tag::I128Small => delegate_compare_to_wrapper!(I128Small, a, b, self),

                Tag::U256Small => delegate_compare_to_wrapper!(U256Small, a, b, self),
                Tag::I256Small => delegate_compare_to_wrapper!(I256Small, a, b, self),

                Tag::SymbolSmall => delegate_compare_to_wrapper!(SymbolSmall, a, b, self),

                Tag::SmallCodeUpperBound => Ok(Ordering::Equal),
                Tag::ObjectCodeLowerBound => Ok(Ordering::Equal),

                // None of the object cases should be reachable, they
                // should all have been handled by the is_object() branch
                // above.
                Tag::U64Object
                | Tag::I64Object
                | Tag::TimepointObject
                | Tag::DurationObject
                | Tag::U128Object
                | Tag::I128Object
                | Tag::U256Object
                | Tag::I256Object
                | Tag::BytesObject
                | Tag::StringObject
                | Tag::SymbolObject
                | Tag::VecObject
                | Tag::MapObject
                | Tag::AddressObject => Err(self.error_from_error_val(Error::from_type_and_code(
                    ScErrorType::Context,
                    ScErrorCode::InternalError,
                ))),

                Tag::ObjectCodeUpperBound => Ok(Ordering::Equal),
                Tag::Bad => Ok(Ordering::Equal),
            }
        }
    }
}