soroban_env_common/
object.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
use crate::xdr::{Duration, ScVal, TimePoint};
use crate::{
    impl_val_wrapper_base, num, val::ValConvert, Compare, Convert, Env, Tag, TryFromVal, Val,
};
use core::{cmp::Ordering, fmt::Debug};

/// Wrapper for a [Val] that is tagged with one of the object types,
/// interpreting the [Val]'s body as containing a 32-bit handle to a host
/// object.
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct Object(pub(crate) Val);
impl_val_wrapper_base!(Object);

impl ValConvert for Object {
    fn is_val_type(v: Val) -> bool {
        v.is_object()
    }

    unsafe fn unchecked_from_val(v: Val) -> Self {
        Object(v)
    }
}

impl Object {
    #[inline(always)]
    pub const fn get_handle(&self) -> u32 {
        self.as_val().get_major()
    }

    #[inline(always)]
    pub const fn from_handle_and_tag(handle: u32, tag: Tag) -> Self {
        debug_assert!(tag.is_object());
        unsafe { Object(Val::from_major_minor_and_tag(handle, 0, tag)) }
    }
}

impl<E: Env> Compare<Object> for E {
    type Error = E::Error;

    fn compare(&self, a: &Object, b: &Object) -> Result<Ordering, Self::Error> {
        self.compare(&a.to_val(), &b.to_val())
    }
}

/// `ScValObject` (and its reference-based type `ScValObjRef`) is a small
/// wrapper type that does _not_ have its own XDR definition, it just denotes
/// (as a type) the subset of `ScVal` values that need to be represented in
/// `Val` by one of the cases that can be an `Object`. In other words
/// `Val::try_from_val(&v, e).is_object()` will be true iff
/// `ScValObject::classify(v)` is `Ok(ScValObject(v))`.

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ScValObject(ScVal);
impl From<ScValObject> for ScVal {
    fn from(value: ScValObject) -> Self {
        value.0
    }
}

impl<E> TryFromVal<E, Object> for ScValObject
where
    E: Env + Convert<Object, ScValObject>,
{
    type Error = <E as Convert<Object, ScValObject>>::Error;
    fn try_from_val(env: &E, val: &Object) -> Result<Self, Self::Error> {
        env.convert(*val)
    }
}

impl<'a, E> TryFromVal<E, ScValObjRef<'a>> for Object
where
    E: Env + Convert<ScValObjRef<'a>, Object>,
{
    type Error = crate::Error;

    fn try_from_val(env: &E, v: &ScValObjRef<'a>) -> Result<Self, Self::Error> {
        match env.convert(*v) {
            Ok(obj) => Ok(obj),
            Err(e) => Err(e.into()),
        }
    }
}

impl ScValObject {
    /// Inspect the provided `value` and return `Ok(ScValObject(value))` if it
    /// is a value that should be represented as an object, else `Err(value)`.
    pub fn classify(value: ScVal) -> Result<ScValObject, ScVal> {
        if ScValObjRef::classify(&value).is_some() {
            Ok(ScValObject(value))
        } else {
            Err(value)
        }
    }

    /// Assert that `value` is the sort of value that should be
    /// `ScValObject` without actually inspecting its value.
    pub unsafe fn unchecked_from_val(value: ScVal) -> ScValObject {
        ScValObject(value)
    }
}

impl AsRef<ScVal> for ScValObject {
    fn as_ref(&self) -> &ScVal {
        &self.0
    }
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ScValObjRef<'a>(&'a ScVal);

impl<'a> From<ScValObjRef<'a>> for &'a ScVal {
    fn from(val: ScValObjRef<'a>) -> Self {
        val.0
    }
}

impl AsRef<ScVal> for ScValObjRef<'_> {
    fn as_ref(&self) -> &ScVal {
        self.0
    }
}

impl<'a> ScValObjRef<'a> {
    /// Return either `Some(ScValObject(value))` if the provided `value` is a case
    /// that needs to be stored as a host-side object, or else `None`.
    pub fn classify(value: &'a ScVal) -> Option<Self> {
        match value {
            // Always-small values are never ScValObject, nor are
            // ScVals that don't actually project into Vals at all.
            ScVal::Bool(_)
            | ScVal::Void
            | ScVal::Error(_)
            | ScVal::U32(_)
            | ScVal::I32(_)
            | ScVal::LedgerKeyContractInstance
            | ScVal::LedgerKeyNonce(_)
            | ScVal::ContractInstance(_) => None,

            // Always-large values are always ScValObject
            ScVal::Bytes(_)
            | ScVal::String(_)
            | ScVal::Vec(_)
            | ScVal::Map(_)
            | ScVal::Address(_) => Some(ScValObjRef(value)),

            // Other values are small or large depending on
            // their actual scalar value.
            ScVal::U64(u) | ScVal::Timepoint(TimePoint(u)) | ScVal::Duration(Duration(u)) => {
                if num::is_small_u64(*u) {
                    None
                } else {
                    Some(ScValObjRef(value))
                }
            }
            ScVal::I64(i) => {
                if num::is_small_i64(*i) {
                    None
                } else {
                    Some(ScValObjRef(value))
                }
            }
            ScVal::U128(u) => {
                if num::is_small_u128(u.into()) {
                    None
                } else {
                    Some(ScValObjRef(value))
                }
            }
            ScVal::I128(i) => {
                if num::is_small_i128(i.into()) {
                    None
                } else {
                    Some(ScValObjRef(value))
                }
            }
            ScVal::U256(u) => {
                if num::is_small_u256_parts(u) {
                    None
                } else {
                    Some(ScValObjRef(value))
                }
            }
            ScVal::I256(i) => {
                if num::is_small_i256_parts(i) {
                    None
                } else {
                    Some(ScValObjRef(value))
                }
            }
            ScVal::Symbol(s) => {
                if s.len() <= crate::symbol::MAX_SMALL_CHARS {
                    None
                } else {
                    Some(ScValObjRef(value))
                }
            }
        }
    }
}